A naïve seeded pseudo-random number generator for JS. This is mainly meant for testing purposes: the method used is reportedly ~2-3 times slower than Math.random()
.
- Should allow for multiple independent generators (testing required).
- Array shuffle function included (uses what will be a slow implementation, leveraging
reduce
)
npm install
into project.
// import all functions under the `Random` namespace:
import Random from 'sutor-random'
// use like:
Random.seeded()
Random.intBetween(10, 20)
// import specific functions
import { seeded, intBetween} from 'sutor-random'
// use like:
seeded()
intBetween(10, 20)
Returns a random value between zero and one inclusive. Functionally identical to Math.random()
. If the optional seed is passed, values become predictable.
> Random.seeded(1)
0.7098480789645691
Returns a random integer between min and max inclusive. If the optional seed is passed, values become predictable.
> Random.intBetween(10, 20, 1)
17
Returns a random integer between zero and max inclusive. If the optional seed is passed, values become predictable.
> Random.intBetween(20, 1)
14
Shuffles an array randomly, using Random.seeded()
+ a privately-defined shuffle function. If the optional seed is passed, values become predictable.
> Random.shuffle([1,2,3,4,5,6], 20)
[2,4,6,3,1,5]