https://github.com/remko/lcg-random
(Predictable) LCG Random Number Generator
https://github.com/remko/lcg-random
Last synced: 4 months ago
JSON representation
(Predictable) LCG Random Number Generator
- Host: GitHub
- URL: https://github.com/remko/lcg-random
- Owner: remko
- Created: 2014-11-07T18:32:02.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-06-10T11:51:59.000Z (about 10 years ago)
- Last Synced: 2025-02-07T19:07:43.537Z (over 1 year ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# [lcg-random: (Predictable) LCG Random Number Generator](https://el-tramo.be/lcg-random)
Creates a [Linear Congruential Generator](http://en.wikipedia.org/wiki/Linear_congruential_generator)
for generating random numbers. The random numbers are predictable/reproducable, which is useful for
(unit) testing purposes.
## Installation
npm install lcg-random --save
## Usage
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());
## API
### `lcgRandom(options)`
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](https://en.wikipedia.org/wiki/Lehmer_random_number_generator).
- **`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
## Project Status
[](https://travis-ci.org/remko/lcg-random)
[](https://coveralls.io/r/remko/lcg-random?branch=master)
[
](https://ci.testling.com/remko/lcg-random)
## Changelog
### 2.0.0 (2016-06-10)
- Allow increment of 0 (enabling Lehmer RNGs) ([\#1](https://github.com/remko/lcg-random/issues/1))
- Use [MINSTD](https://en.wikipedia.org/wiki/Lehmer_random_number_generator) as default values ([\#1](https://github.com/remko/lcg-random/issues/1))
- Change default `seed` to 1 (to enable MINSTD)
- Add bounds checks
### 1.0.2 (2014-11-09)
- Fix `index` in `package.json`
### 1.0.1 (2014-11-07)
- Initial version