lr

lcg-random

(Predictable) LCG Random Number Generator

Showing:

Popularity

Downloads/wk

1

GitHub Stars

1

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

0

Size (min+gzip)

0.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

lcg-random: (Predictable) LCG Random Number Generator

Creates a 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.

  • 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

Build Status

Coverage Status

Browser Support

Changelog

2.0.0 (2016-06-10)

  • 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

1.0.2 (2014-11-09)

  • Fix index in package.json

1.0.1 (2014-11-07)

  • Initial version

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial