This is a
playground to test code. It runs a full
Node.js environment and already has all of
npm’s 400,000 packages pre-installed, including
js_mt_rand with all
npm packages installed. Try it out:
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.
A pseudo-random generator that can produce the same numbers as php's mt_rand do with given seed.
$ npm install js_mt_rand
$ yarn add js_mt_rand
import JSMTRand from 'js_mt_rand'; let mt = new JSMTRand(); // seed the generator mt.srand(0); // php 7.1+ Mersenne Twister implementation (default) mt.srand(0, JSMTRand.MODE_MT_RAND_19937); // get next random number, range: [0, 2 ^ 32 - 1) // N.B. MODE_MT_RAND_19937 has a wider range than MODE_MT_RAND_PHP let m = mt.rand(0, 0xFFFFFFFF); // php 5.x backward compatibility. mt.srand(0, JSMTRand.MODE_MT_RAND_PHP); // get next random number, range: [0, 2 ^ 31 - 1] let n = mt.rand(); // get next random number in range: [min, max], max is inclusive let r = mt.rand(min, max);
Due to the PHP 7.1.0 to 7.2.0beta2 mt_rand() modulo bias bug, the
JSMTRand.rand(min, max) may return different results between php 7.1.0 to 7.2.0beta2, just use
JSMTRand.rand() instead and wrap your own range function.
The MIT License (MIT)
Authors of PHP 7.1+: Rasmus Lerdorf, Zeev Suraski, Pedro Melo, Sterling Hughes.
Based on code from: Richard J. Wagner, Makoto Matsumoto, Takuji Nishimura, Shawn Cokus.