Skip to content

add seeded prng: Math.seedRandom() and lib/random.ts lcg#75

Closed
cs01 wants to merge 1 commit intomainfrom
worktree-seeded-prng
Closed

add seeded prng: Math.seedRandom() and lib/random.ts lcg#75
cs01 wants to merge 1 commit intomainfrom
worktree-seeded-prng

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 2, 2026

Summary

  • Add Math.seedRandom(seed) to codegen — converts double seed to i64 and calls srand48, making subsequent Math.random() calls deterministic for a given seed
  • Add lib/random.ts — standalone Random class implementing the Numerical Recipes LCG (state = state * 1664525 + 1013904223 mod 2^32), safe within double precision

API

// Seed the global Math.random()
Math.seedRandom(42);
const r = Math.random(); // deterministic

// Standalone seeded RNG
import { Random } from "../lib/random.js";
const rng = new Random(12345);
rng.next();           // [0, 1)
rng.nextInt(0, 10);   // integer in [0, 10)

Test plan

  • math seed random — same seed produces same sequence, different seed produces different value
  • random lib basic — reproducibility, range checks, nextInt bounds

@cs01 cs01 closed this Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant