generative99 / simple-random-ts

Seedable random number generator in Typescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

simple-random-ts

Seedable random number generator in Typescript for Deno

The Mersenne Twister is a pseudo-random number generator invented by Makoto Matsumoto in 1997. Details can be found on the Wikipedia page and on Matsumoto's website.

This implementation is based on Sean McCullough's port of the original C code written by Makato Matsumoto and Takuji Nishimura and Raphael Pigulla's continuation.

Improvements over Sean's version are

  • more idiomatic, jshint-compliant and jsdoc-annotated code
  • compatible with Node.js, requirejs and browser environments
  • available as a module for npm, Jam and Bower
  • (somewhat) unit tested ;-)

Please note that the mersenne twister is not cryptographically secure.

Usage

Instantiate your own instance of the mersenne twister and use its methods:

const mt = new MersenneTwister(seed); // if no seed is defined, seed randomly

mt.int();    // random 32-bit integer
mt.int31();  // random 31-bit integer

mt.rnd();       // random float in the interval [0;1[ with 32-bit resolution
mt.random();    // random float in the interval [0;1[ (same as mt.rnd() above)
mt.rndHiRes();  // random float in the interval [0;1[ with 53-bit resolution
mt.real();      // random float in the interval [0;1]
mt.realx();     // random float in the interval ]0;1[

mt.seed(seed);      // (re)seed the generator with an unsigned 32-bit integer
mt.seedArray(key);  // (re)seed using a state vector of unsigned 32-bit integers

Note: that the seed only affects output in integer increments.

Take a look at the inventor´s website if more detailed information is required.

Licensing

As indicated here, the Mersenne Twister algorithm is free to be used for any purpose, including commercial use. The license file of this module contains the BSD 3-clause license found in the C implementation on which it is based.

About

Seedable random number generator in Typescript

License:MIT License


Languages

Language:TypeScript 100.0%