Seeded Pseudo-Random Generator for Tests
Produce reproducible pseudo-random numbers with Mulberry32, plus integer range and array pick helpers for deterministic fixtures.
Code
export interface SeededRandom {
next(): number;
int(min: number, max: number): number;
pick<T>(items: readonly T[]): T;
}
export function createSeededRandom(seed: number): SeededRandom {
if (!Number.isInteger(seed)) throw new TypeError("seed must be an integer");
let state = seed >>> 0;
const next = (): number => {
state = (state + 0x6d2b79f5) >>> 0;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
return {
next,
int(min: number, max: number): number {
if (!Number.isInteger(min) || !Number.isInteger(max) || max < min) {
throw new RangeError("min and max must define an integer range");
}
return min + Math.floor(next() * (max - min + 1));
},
pick<T>(items: readonly T[]): T {
if (!Array.isArray(items) || items.length === 0) {
throw new RangeError("items must be a non-empty array");
}
return items[Math.floor(next() * items.length)] as T;
},
};
}How it works
Count the functions next and createSeededRandom as the entire surface area of Seeded Pseudo-Random Generator for Tests.
The failure path is explicit — TypeError and RangeError is raised when the guard condition holds.
The result is a fresh construction, so callers can keep their originals.
Using it
The main signature reads export function createSeededRandom(seed: number)
createSeededRandom is what another module would consume; nothing else here is load-bearing for callers.
Key statements
if (!Number.isInteger(seed)) throw new TypeError("seed must be an integer");
throw new RangeError("min and max must define an integer range");
throw new RangeError("items must be a non-empty array");
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.JSONPath TesterTest JSONPath expressions against JSON documents with dot notation, brackets, wildcards, and recursive descent.Passphrase GeneratorGenerate memorable multi-word passphrases from a fixed 150-word list with crypto randomness, separators, and capitalization options.Password GeneratorGenerate random passwords from selectable character classes using crypto.getRandomValues with rejection sampling and class guarantees.
FAQ
- Does `createSeededRandom` modify its input?
- No mutation happens — the function reads its input and returns a new value.
- How does the snippet fail?
- Validation comes first — `TypeError` and `RangeError` marks the failure, before any partial work happens.
- Is anything beyond the standard library required?
- Nothing beyond the standard library — the snippet is dependency-free. The file defines `next`, `createSeededRandom` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.