Creates a Linear Congruential Generator for generating random numbers. The random numbers are predictable/reproducable, which is useful for (unit) testing purposes.
npm install lcg-random --save
A call to the exported function returns a function that generates a random number on every call:
var lcgRandom = require("lcg-random");
// Outputs 0.000007826369259425611 0.13153778814316625 0.7556053221950332
var rand1 = lcgRandom();
console.log(rand1(), rand1(), rand1());
// Also outputs 0.000007826369259425611 0.13153778814316625 0.7556053221950332
var rand2 = lcgRandom();
console.log(rand2(), rand2(), rand2());
Returns a function that returns a random number between 0 and 1 every time it is called.
The function used is
Xn+1 = (multiplier * Xn + increment) % modulus
Every component of the function can be customized by setting it in the options
argument.
The default values are the ones from Park and Miller's MINSTD.
-
options.seed
- number (0 ≤options.seed
<options.modulus
)
Seed (start value) for the generator.
Default: 1 -
options.modulus
- modulus (0 <options.modulus
)
Modulus for the generator.
Default: 231-1 -
options.multiplier
- modulus (0 <options.multiplier
<options.modulus
)
Multiplier for the generator.
Default: 75 -
options.increment
- modulus (0 ≤options.increment
<options.modulus
)
Increment for the generator.
Default: 0
- Allow increment of 0 (enabling Lehmer RNGs) (#1)
- Use MINSTD as default values (#1)
- Change default
seed
to 1 (to enable MINSTD) - Add bounds checks
- Fix
index
inpackage.json
- Initial version