Replayable Random enables to generate reproducible sequences of pseudo-random numbers. The librray provides several seeded generators and multiple distributions.
Choose an API
Replayable Random provides both a pure functional API and an imperative API. The pure functional API is well-suited for projects that embrace immutability and purity. It uses a copy-on-write startegy to achieve better performances.
Pay only for what you use
Replayable Random is designed to take advantage of modern dead code elimination techniques, especially tree shaking. Using bundlers such as rollup, your bundles can contain only the functions which are actually used. Future versions of Replayable Random can integrate new functions without affecting the size of your bundles.
Replayable Random have well-defined interfaces and provides hightly composable helpers and distributions.
Install replayable-random as a dependency:
npm install replayable-random
Choose a random generator
Import a genrator using its name, e.g.
Derive the first state from a string (printable ASCII) seed:
// Pure functional APIconst g = alea// Imperative APIconst mutG = alea
Choose a distribution
Choose a distribution and generate your first random numbers. e.g. to generate two integers between -4 (inclusive) and 5 (excluded):
// Pure functional APIconst n1 g1 = distrib5gconst n2 g2 = distrib5g1// Imperative APIlet nn = distrib5mutGn = distrib5mutG// Math.random compatibilityn = distrib5Mathn = distrib5Math