The seeded generator
Random numbers you can have again. The same seed gives the same stream on every machine, whole numbers are exactly uniform at any size, and probabilities are exact fractions.
Making one
import { rng } from "@ratmath/discrete";
const g = rng(2026); // a whole number of any size
const h = rng("experiment 3"); // or text
g.u32(); // 3023761063: always, for this seed
g.u32(); // 2983606895
JavaScript's Math.random() cannot be seeded, so a simulation built on it can never be rerun. Here every result is a function of the seed alone. The first values for a given seed are fixed by the tests of this library, and will not change between versions.
Whole numbers, exactly uniform
g.int(6); // 0…5, each with probability exactly 1/6
g.between(1, 6); // a die
g.int(10n ** 40n); // a BigInt below 10⁴⁰, exactly uniform
g.bits(256); // 256 random bits as a BigInt
The common shortcut, Math.floor(Math.random() * n) or random % n, is slightly biased whenever does not divide the size of the generator's range: some values get one more chance than others. int removes the bias by rejection: it draws just enough bits to cover , and if the draw is or more it throws it away and draws again. Each accepted value is then equally likely, and fewer than two draws are needed on average.
Exact probabilities
g.bool(); // a fair coin
g.bool("1/3"); // true with probability exactly 1/3
g.weighted(["red", "green", "blue"], [5, 3, "1/2"]); // weights are exact fractions
g.rational(1000); // a Core Rational k/1000, 0 ≤ k < 1000
g.float(); // a double in [0, 1): the one inexact method
A probability of 1/3 cannot be written as a floating-point number, so comparing a random double with 1/3 is off by a hair. bool("1/3") draws a uniform whole number below 3 and asks whether it is below 1. Weighted choice works the same way, over the common denominator of the weights.
Shuffles and samples
g.shuffle([1, 2, 3, 4, 5]); // a new array; all 120 orders equally likely
g.sample(["a", "b", "c", "d"], 2); // two distinct items, in random order
g.pick(["heads", "tails"]);
import { permutations, partitions } from "@ratmath/discrete";
permutations(52).random(g); // or through any family
partitions(100).random(g); // a uniformly random partition of 100
shuffle is the Fisher–Yates method: for each position from the last, swap with a uniformly chosen position at or before it. It produces each of the orders in exactly one way, so uniform choices give a uniform shuffle. The tempting variant that swaps every position with any position has equally likely runs, which cannot be spread evenly over orders.
Independent streams
const noise = g.fork("noise");
const data = g.fork("data"); // a second stream, unrelated to the first
g.clone(); // a copy that will produce the same future
new Random(g.state); // resume from saved state: four 32-bit numbers
A simulation with one shared generator is fragile: add one extra draw in one place and every later number changes. fork(label) gives each part its own stream, determined by the parent's state and the label, without advancing the parent. Then changing how the noise is drawn leaves the data as it was.
What is inside
The generator is xoshiro128**, a small, fast, well-tested generator with a period of , seeded by hashing the seed and spreading it with SplitMix32. It uses only 32-bit integer operations, which every JavaScript engine performs identically; that is where the reproducibility comes from. It is a tool for simulation and sampling. It is not for passwords, keys or anything an adversary should not predict.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.