A module for generating cryptographically secure pseudo-random numbers.
This module is based on code originally written by Scott Arciszewski, released under the WTFPL / CC0 / ZAP.
Sync:
const { randomSync } = require('pure-random-number')
const n = randomNumber(10, 30)
console.log('Secure unbiased random number:', n)
Async:
const randomNumber = require('pure-random-number')
randomNumber(10, 30)
.then(number => console.log('Your number is:', number))
.catch(err => console.error(err))
Same as randomNumber(minimum, maximum)
except blocks
until a sufficiently random number has been aquired.
Returns a Promise that resolves to a random number within the specified range.
Note that the range is inclusive, and both numbers must be integer values. It is not possible to securely generate a random value for floating point numbers, so if you are working with fractional numbers (eg. 1.24
), you will have to decide on a fixed 'precision' and turn them into integer values (eg. 124
).
- minimum: The lowest possible value in the range.
- maximum: The highest possible value in the range. Inclusive.
- 2.1.0 (June 12, 2020): Added sync version.
- 2.0.0 (May 3, 2020): Removed dependencies and ported to standardjs
- 1.0.2 (March 8, 2016): Security release! Patched handling of large numbers; input values are now checked for
MIN_SAFE_INTEGER
andMAX_SAFE_INTEGER
, and the correct bitwise operator is used (>>>
rather than>>
). - 1.0.1 (March 8, 2016): Unimportant file cleanup.
- 1.0.0 (March 8, 2016): Initial release.
Be aware that by making a pull request, you agree to release your modifications under the license stated below.
Parent license(s) permit change of terms for derivative works.
Thus I now proclaim the license for this repository to be limited to
GNU AGPL version 3
AGPL prevents non-open parties from doing what I just did