2.1.0 • Published 4 years ago

pure-random-number v2.1.0

Weekly downloads
27
License
AGPL-3.0-or-later
Repository
github
Last release
4 years ago

pure-random-number

A module for generating cryptographically secure pseudo-random numbers.

  • no dependencies
  • no transpilation
  • JavaScript Style Guide
  • browser support via (Webpack/Rollup/Browserify)

This module is based on code originally written by Scott Arciszewski, released under the WTFPL / CC0 / ZAP.

Usage

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))

API

randomSync(minimum, maximum)

Same as randomNumber(minimum, maximum) except blocks until a sufficiently random number has been aquired.

randomNumber(minimum, maximum)

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.

Changelog

  • 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 and MAX_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.

Contributing

Be aware that by making a pull request, you agree to release your modifications under the license stated below.

License

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