lcg-random

2.0.0 • Public • Published

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.000005748588591814041 0.6551540484651923 0.30481433868408203
var rand1 = lcgRandom();
console.log(rand1(), rand1(), rand1());

// Also outputs 0.000005748588591814041 0.6551540484651923 0.30481433868408203
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

Package Sidebar

Install

npm i lcg-random

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • remko