This is a concise implementation of the Mersenne Twister Pseudo-Random Number Generator (PRNG) for JavaScript.
The library:
- Includes initialization improvements to the algorithm that were made in 2022
- Is tested against the original C implementation
- Includes TypeScript types
The Mersenne Twister is not a cutting-edge PRNG. It is not cryptographically secure. It is very widely used, being the default PRNG for Excel, R, Python, Ruby and others. It is not without flaws.
- The key use case is for reproducible pseudo-random sequences, which you get by manually seeding your PRNG
- You could also use this because you want your random numbers generated the same way across all browsers and JavaScript engines
x = Math.random();
becomes
import MersenneTwister from 'mtwist';
seed = 1234567890; // an integer between 0 and 4294967295
m = new MersenneTwister(seed);
x = m.random();
Methods to produce evenly distributed integers in the ranges [0,n) and [m,n] are also provided.
For details, see index.ts.
© Copyright 2014 – 2021 George MacKerron and released under the MIT Licence.