A library that wraps the Node.js crypto functions to create random floats and bounded integers with ease.
Warning: I am not a cryptographer, or any sort of random number expert. An audit would be greatly appreciated.
Version 3.0 of this module brings a new API and very different performance characteristics for integers; however it also removes bias which existed in older versions of this module.
To install the module for use in your projects:
npm install random-lib
var rand =var opts =min: 1max: 10num: 10// get 10 random integers, between 1 and 10rand// or, with promisesrand// or synchronouslyvar results = randconsole // [ 2, 1, 3, 8, 9, 7, 2, 4, 4, 7 ]
Options are accepted, but are different if you're asking for floats or integers.
Options are passed via an object; what's shown below are the defaults, and nothing is required.
// for integers; what's shown are the defaults.var options =num: 10 // number of ints to receivemin: 0 // minimum bound (inclusive)max: NumberMAX_SAFE_INT // maximum bound (exclusive),unique: false // receive only unique ints; only supported when asyncrand//for floats; what's shown are the defaults.var options =num: 10 // number of floats to receiveunique: false // receive only unique floats; only supported when asyncrand
When calling any function, omitting a callback will cause the function to return a Promise.
ints([options], [callback (err, results)]): number
Get an array of random integers.
int([options], [callback (err, result)]): number
Convenience function to get a single random integer.
floats([options], [callback (err, results)]): number
Get an array of random floats between 0 and 1.
float([options], [callback (err, results)]): number
Convenience function to get a single random float between 0 and 1.
Synchronous methods take the same options as their async counterparts, except
unique option which is not supported while synchronous.
- Using the synchronous interface calls crypto.randomBytes synchronously; please be sure to read the documentation for your Node.js version to understand the performance implications.
Feel free to send pull requests! I'm not picky, but would like the following:
- Write tests for any new features, and do not break existing tests.
- Follow existing code style.
- Be sure to point out any changes that break API.
- Do not bump package.json
versionor add new dependencies without discussing in an issue first.
See CHANGELOG for details.
MIT. See LICENSE for details.