whrandom / random.js
This isn't the best random number generator, but the algorithm is very simple.
Mersenne Twister / mersenne.js
Python now prefers the Marsenne Twister. It has some nice features over whrandom (you can read that page to see them).
I've taken a piece of code written by Y. Okada and library-ized it some. It matches the interface of whrandom.
If you include
random.js it will expose one variable:
MersenneRandomStream). You can also require the module using requirejs or import it in Node (though I haven't provided a pacakge.json), and in both cases the exported object is
RandomStream itself (e.g.,
var RandomStream = require("./random.js");, ditto
To create a stream, call
RandomStream(seed); if no
seed is provided then
Date.now() is used. To get new random numbers from a stream, simply call the function. All random numbers are between 0 and 1 (but never actually 1), just like
MersenneRandomStream instances also have the methods:
.int31(): generates a random number on [0,0x7fffffff]-interval
.real1(): generates a random number on [0,1]-real-interval
.real2(): generates a random number on [0,1)-real-interval (the default)
.real3(): generates a random number on (0,1)-real-interval
.res53(): generates a random number on [0,1) with 53-bit resolution (basically higher resolution version of the default)