pseudo-math-random
v1.0.0
Published
Same algorithm as Math.random but with a custom seed
Downloads
6
Maintainers
Readme
pseudo-math-random
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).
Usage
const pmr = require('pseudo-math-random')
const random = pmr('a seed')
console.log(random()) // Always 0.32911525012180043
console.log(random()) // Always 0.5563213847156248
API
random = pmr([seed])
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.
num = random()
Get a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). Does not provide cryptographically secure random numbers.
Install
With npm do:
npm install pseudo-math-random
Benchmark
$ 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.
License
MIT © 2019-present Vincent Weevers