/mersenne-twister

A native JS implementation of the Mersenne Twister PRNG.

Primary LanguageJavaScriptMIT LicenseMIT

mersenne-twister

A JavaScript implementation of the Mersenne Twister pseudo-random number generator.

npm version Build Status Coverage Status

How To Use

Installation (via npm)

$ npm install @dsibilly/mersenne-twister

Usage

Get an instance of RNG:

const MersenneTwister = require('@dsibilly/mersenne-twister'),
  rng = = new MersenneTwister();

...or with ES2015+ import:

import MersenneTwister from '@dsibilly/mersenne-twister';

const rng = new MersenneTwister();

Seeding the RNG

const rng2 = new MersenneTwister(4567),
    rng3 = new MersenneTwister([
        // You can also seed with an array of values
        123,
        456,
        789
    ]);

Generating Numbers

Generate a random 32-bit integer:

const result = rng.randomInt();

Generate a random 31-bit integer::

const result = rng.randomInt31();

Generate a random number between 0 and 1, exclusive (e.g. 0 < n < 1):

const result = rng.randomExclusive();

Generate a random number where 0 <= n < 1:

const result = rng.random();

Generate a random number between 0 and 1, inclusive (e.g. 0 <= n <= 1):

const result = rng.randomInclusive();

Generate a random 53-bit number, 0 <= n <= 1:

const result = rng.randomLong();

License

MIT License

Maintainer

Duane Sibilly