3 ms·
If anyone wants a seedable, and therefore deterministic PRNG, use the seedrandom package https://www.npmjs.com/package/seedrandom https://www.npmjs.com/package/
by somewhereoutth 2y ago
If anyone wants a seedable, and therefore deterministic PRNG, use the seedrandom package https://www.npmjs.com/package/seedrandom https://www.npmjs.com/package/seedrandom
// Global PRNG: set Math.random.
seedrandom('hello.', { global: true });
console.log(Math.random()); // Always 0.9282578795792454
I found this useful when using worker threads to 'render' a construction by slices that included some randomness (that should be the same for each thread)
- svachalek 2y agoReplacing a system function is a quick and easy hammer but not great for maintenance. It's not clear that calls to Math.random() are now actually going to an npm, and if someone deletes that seedrandom line it's not clear that calls to Math.random() are now not actually going to an npm. For new/clean code it's better to just import a seeded random function for clarity.