Files
common/packages/util-crypto/src/random/asNumber.ts
T
Jaco e8af9b5a1e Add a helper for de-dupe of *AsHex (#1224)
* Add a helper for de-dupe of *AsHex

* Update packages/util-crypto/src/random/asU8a.ts

* lint

* isWasmOnly helper

* bit hasher
2021-11-21 20:21:20 +02:00

29 lines
700 B
TypeScript

// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { BN, hexToBn } from '@polkadot/util';
import { randomAsHex } from './asU8a';
const BN_53 = new BN(0b11111111111111111111111111111111111111111111111111111);
/**
* @name randomAsNumber
* @summary Creates a random number from random bytes.
* @description
* Returns a random number generated from the secure bytes.
* @example
* <BR>
*
* ```javascript
* import { randomAsNumber } from '@polkadot/util-crypto';
*
* randomAsNumber(); // => <random number>
* ```
*/
export function randomAsNumber (): number {
return hexToBn(
randomAsHex(8)
).and(BN_53).toNumber();
}