Files
common/packages/util-crypto/src/random/asNumber.ts
T
2020-09-17 16:13:39 +02:00

30 lines
724 B
TypeScript

// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { hexToBn } from '@polkadot/util';
import randomAsHex from './asHex';
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 default function randomAsNumber (): number {
return hexToBn(
randomAsHex(8)
).and(BN_53).toNumber();
}