This is a cross-platform implementation of the Mersenne Twister pseudorandom number generator algorithm (MT19937) that produces identical sequences in JavaScript, Python, and C# when initialized with the same seed.
- Consistent random number generation across JavaScript, Python, and C#
- Support for seeding to reproduce random sequences
- High-quality randomness with a period of 2^19937-1
- API for integers, floats and ranges
The implementation is based on the original Mersenne Twister algorithm by Makoto Matsumoto and Takuji Nishimura. All three implementations follow the same algorithm with identical constants and operations to ensure they produce the same sequences given the same seed.
// Import the class
const MersenneTwister = require('./MersenneTwister');
// Create a new instance with a seed
const rng = new MersenneTwister(12345);
// Generate random values
console.log(rng.nextInt32()); // Random 32-bit integer
console.log(rng.random()); // Random float in [0,1)
console.log(rng.randInt(1, 100)); // Random integer between 1 and 100
console.log(rng.randDouble(0, 10)); // Random float between 0 and 10
// Reset the seed
rng.seed(67890);# Import the class
from MersenneTwister import MersenneTwister
# Create a new instance with a seed
rng = MersenneTwister(12345)
# Generate random values
print(rng.next_int32()) # Random 32-bit integer
print(rng.random()) # Random float in [0,1)
print(rng.rand_int(1, 100)) # Random integer between 1 and 100
print(rng.rand_double(0, 10)) # Random float between 0 and 10
# Reset the seed
rng.seed(67890)// Import the namespace
using MersenneTwisterRNG;
// Create a new instance with a seed
var rng = new MersenneTwister(12345);
// Generate random values
Console.WriteLine(rng.NextInt32()); // Random 32-bit integer
Console.WriteLine(rng.Random()); // Random float in [0,1)
Console.WriteLine(rng.RandInt(1, 100)); // Random integer between 1 and 100
Console.WriteLine(rng.RandDouble(0, 10)); // Random float between 0 and 10
// Reset the seed
rng.Seed(67890);