Get a random buffer based on a seed. Fast, not secure.
Useful for reproducible tests and benchmarks. Same API asrandom-bytes-seed
which usescrypto
(secure) while this uses xorshift128+ (10-25x faster). See alsopseudo-math-random
.
const prb = require('pseudo-random-buffer')
const randomBytes = prb('a seed')
console.log(randomBytes(4)) // <Buffer a3 e5 40 54>
console.log(randomBytes(3)) // <Buffer 09 14 6b>
This example will always log the same bytes, unless you change the seed.
Create a new random bytes 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 buffer with pseudo random bytes.
With npm do:
npm install pseudo-random-buffer
$ node benchmark.js 1024
node v10.14.1, n=1024
pseudo-random-buffer x 166,796 ops/sec ±1.68% (86 runs sampled)
random-bytes-seed x 13,477 ops/sec ±2.08% (76 runs sampled)
Fastest is pseudo-random-buffer
NB. Speed isn't everything. Decide for yourself which properties you need.
MIT © 2019-present Vincent Weevers