UNPKG

577 BJavaScriptView Raw
1import { BN, hexToBn } from '@polkadot/util';
2import { randomAsHex } from './asU8a.js';
3const BN_53 = new BN(0b11111111111111111111111111111111111111111111111111111);
4/**
5 * @name randomAsNumber
6 * @summary Creates a random number from random bytes.
7 * @description
8 * Returns a random number generated from the secure bytes.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { randomAsNumber } from '@polkadot/util-crypto';
14 *
15 * randomAsNumber(); // => <random number>
16 * ```
17 */
18export function randomAsNumber() {
19 return hexToBn(randomAsHex(8)).and(BN_53).toNumber();
20}