Replayable Random enables to generate reproducible sequences of pseudo-random numbers. The librray provides several seeded generators and multiple distributions.
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.
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
Import a genrator using its name, e.g. alea
:
import { alea } from "replayable-random"
Derive the first state from a string (printable ASCII) seed:
// Pure functional API
const g = alea.from("printable-ascii-seed")
// Imperative API
const mutG = alea.mutFrom("printable-ascii-seed")
Import distrib
:
import { distrib } from "replayable-random"
Choose a distribution and generate your first random numbers. e.g. to generate two integers between -4 (inclusive) and 5 (excluded):
// Pure functional API
const [n1, g1] = distrib.i32Between(-4)(5)(g)
const [n2, g2] = distrib.i32Between(-4)(5)(g1)
// Imperative API
let n
n = distrib.mutI32Between(-4)(5)(mutG)
n = distrib.mutI32Between(-4)(5)(mutG)
// Math.random compatibility
n = distrib.mutI32Between(-4)(5)(Math)
n = distrib.mutI32Between(-4)(5)(Math)
- Available PRNGs
- Available distributions
- Best practices
- Write your own PRNG (coming soon)
- Write your own distribution (coming soon)