Super Simple Seeded Random
let _seed = 1234567;
// Deterministic pseudo-random float in the interval [ 0, 1 ]
function seededRandom( s ) {
if ( s !== undefined ) _seed = s % 2147483647;
// Park-Miller algorithm
_seed = _seed * 16807 % 2147483647;
return ( _seed - 1 ) / 2147483646;
}
Straight from the THREE.js source code – a fun and simple seeded random. The best libraries are always filled with gems like this…
If I were going to use this I would do a couple things out of pure preference:
let seed = 1234567;
const setSeed = newSeed => seed = newSeed
function seededRand() {
// Park-Miller algorithm
seed *= 16807 % 0x7fffffff;
return (seed - 1) / 0x7fffffff;
}
// try it out:
console.log('one', seededRand());
console.log('two', seededRand());
console.log('three', seededRand());
seed = 9999
console.log('one new seed', seededRand());
console.log('one new seed', seededRand());
seed = 1234567;
console.log('one old seed', seededRand());
If you’re wondering about what this is doing… read more about it here.