Same algorithm as Math.random but with a custom seed.
Useful for reproducible tests and benchmarks. Same API asmath-random-seed
which usescrypto
(secure) while this uses xorshift128+ (60x faster).
const pmr = require('pseudo-math-random')
const random = pmr('a seed')
console.log(random()) // Always 0.32911525012180043
console.log(random()) // Always 0.5563213847156248
Create a new random number generator. The seed
argument must be a string or Buffer. It is hashed once to counter short or zero-filled seeds. If no seed is provided one will be generated.
Get a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). Does not provide cryptographically secure random numbers.
With npm do:
npm install pseudo-math-random
$ node benchmark.js
node v10.14.1
pseudo-math-random x 23,050,253 ops/sec ±0.24% (94 runs sampled)
math-random-seed x 377,674 ops/sec ±1.50% (86 runs sampled)
Fastest is pseudo-math-random
NB. Speed isn't everything. Decide for yourself which properties you need.
MIT © 2019-present Vincent Weevers