@dsibilly/mersenne-twister
v2.0.0
Published
A native JS implementation of the Mersenne Twister PRNG.
Downloads
4,351
Readme
mersenne-twister
A JavaScript implementation of the Mersenne Twister pseudo-random number generator.
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();