seedrandom deterministic sequences: working versions (tested 2026-10-16)
Build seeded pseudorandom generators with seedrandom, assert reproducible sequences, verify divergence between seeds, and temporarily patch then restore Math.random offline.
passed5 passed · 0 failed2026-10-16 15:21 UTC- Status
- passed
- Last passed
- 2026-10-16 15:21 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions4
seedrandom@3.0.5@types/seedrandom@3.0.8@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38923127324
The code
Exactly the files that passed. Your agent can pull them with get_recipe("seedrandom-deterministic") (install), or see them on GitHub.
package.json
{
"name": "seedrandom-deterministic",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"seedrandom": "3.0.5"
},
"devDependencies": {
"@types/seedrandom": "3.0.8",
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/rng.ts
import seedrandom from "seedrandom";
export type Rng = () => number;
export function createRng(seed: string): Rng {
return seedrandom(seed) as Rng;
}
export function createAleaRng(seed: string): Rng {
return seedrandom.alea(seed) as Rng;
}
export function take(rng: Rng, count: number): number[] {
return Array.from({ length: count }, () => rng());
}
export function withSeededMath<T>(seed: string, fn: () => T): T {
const originalRandom = Math.random;
const originalSeedrandom = (Math as { seedrandom?: unknown }).seedrandom;
seedrandom(seed, { global: true });
try {
return fn();
} finally {
Math.random = originalRandom;
if (originalSeedrandom === undefined) {
delete (Math as { seedrandom?: unknown }).seedrandom;
} else {
(Math as { seedrandom?: unknown }).seedrandom = originalSeedrandom;
}
}
}
test/rng.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { createRng, createAleaRng, take, withSeededMath } from "../src/rng.ts";
test("same seed produces identical sequences", () => {
const a = take(createRng("alpha"), 5);
const b = take(createRng("alpha"), 5);
assert.equal(a.length, 5);
assert.deepEqual(a, b);
assert.ok(a.every((n) => n >= 0 && n < 1));
});
test("different seeds produce different sequences", () => {
const a = take(createRng("alpha"), 5);
const b = take(createRng("beta"), 5);
assert.notDeepEqual(a, b);
});
test("alea generator is reproducible", () => {
const a = take(createAleaRng("seed-1"), 4);
const b = take(createAleaRng("seed-1"), 4);
assert.deepEqual(a, b);
});
test("withSeededMath patches Math.random then restores it", () => {
const original = Math.random;
const samples = withSeededMath("shared", () => [
Math.random(),
Math.random(),
Math.random(),
]);
assert.equal(Math.random, original);
assert.deepEqual(samples, take(createRng("shared"), 3));
});
test("withSeededMath restores Math.random even on error", () => {
const original = Math.random;
assert.throws(() =>
withSeededMath("boom", () => {
throw new Error("boom");
})
);
assert.equal(Math.random, original);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.