Lightweight, fast implementation of the WELL-1024a PRNG
Install using npm
npm install well-rng
Then, create a new instance of a WELL-1024a generator and use it to produce random numbers:
var WELL = require'well-rng';var rng = ;console.logrngrandInt;
The other obvious case, I suppose, is if you wanted something to replace Math.random() that was higher quality (WELL is relatively new but considered superior to the Mersenne Twister in functional performance and state recovery). I've made attempts at implementing this to be fast, but the implementation of Math.random() is of course still faster. It's still pretty quick though.
// Generates a random integer between 0 and 2^31-1wellrandInt;// Generates a random float between 0 and 1wellrandom;// Generates a random integer between a and b, inclusivewellrandInta b;// Generates a random integer with the specified number of bitswellrandBitsbits;// Obtain the current state vectorwellget_state;well_n; // state pointer, arguably part of the state as well// Set the current state vector, optionally as well as the state pointer, to produce// specific resultswellset_statestate n;// The state vector should be a 32-entry array of 32-bit integers.
In terms of performance, well.randBits() offers the fastest generation for small-ish numbers, because it is able to amortize the cost of running the PRNG, which it does by computing 31 bits at once and then returning them in chunks of the requested sizes. Run the benchmark script, bench.js, to get an idea of the relative performance of well-rng and Math.random() for each usage case.