Skip to content

Same algorithm as Math.random but with a custom seed.

License

Notifications You must be signed in to change notification settings

vweevers/pseudo-math-random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pseudo-math-random

Same algorithm as Math.random but with a custom seed.
Useful for reproducible tests and benchmarks. Same API as math-random-seed which uses crypto (secure) while this uses xorshift128+ (60x faster).

npm status node Travis build status JavaScript Style Guide

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