seedrand.js
is a micro library which allows you to seed Math.random()
. It’s
mainly written for testing functions that require random numbers.
The library can be included server- and/or client-side. It moves the original
Math.random
to Math._random
, and use a custom pseudo-random number generator
(PRNG) to replace it.
<script src="/path/to/seedrand.min.js"></script>
<script>
var mySeed = +new Date(),
a, b;
Math.seed( mySeed );
a = Math.random();
Math.seed( mySeed );
b = Math.random();
console.log( a === b ); // true
</script>
require( 'seedrand' );
var mySeed = +new Date(),
a, b;
Math.seed( mySeed );
a = Math.random();
Math.seed( mySeed );
b = Math.random();
console.log( a === b ); // true
On the client-side, include the seedrand.min.js
file in your page. On the
server-side, install the seedrand
npm package:
[sudo] npm -g install seedrand
The code of the PRNG is based on Adam Hyland’s implementation of “Webkit2’s crazy invertible mapping generator”.