Status: done (2026-11-02)
Priority: 2
Size: M
Arch: core
Phase: implement
Design ref: none (determinism and saves); REVIEW.md, Development, debugging and inspection.
Goal: One random function, st.rand, feeds job matching, weather, births, deaths, fences and attitude drift. A change to one system reshuffles all the others, so a tuning comparison mixes the change with noise, and the closure’s state cannot be saved.
Acceptance criteria:
src/sim/rng.js (@sys sim/rng) makes a random stream as plain data plus functions: makeStream(seed, name) returns { s } (a 32-bit state, mulberry32 as today), and next(stream) returns a number in [0, 1) and advances s. A stream copied with JSON.parse(JSON.stringify(...)) continues identically.st.rng holds one stream per system: work (matching, idling, walking offsets), fields (yields), weather, fences, births, deaths, culture (drift, entrepreneurs), people (names and looks of the newborn), society (marriage match). Each is seeded from the scenario’s seed and its name. @state documented.*/seed and world/* entries of tests/golden.json are unchanged; the yearly entries change, which is intended. Re-record and list the changed entries in the PR.st.rand goes; every call site names its stream. Culture.dailyStep, entrepreneurs and seedAttitude take a function as today, built from the stream.st.rng.work, and check that the births and deaths streams return the same next values as in a run without the extra draws.talk (contacts and talk, sim/relations), herds (sim/herds, sim/byre) and settle (moving to new land, sim/settle). Tests that make every chance come up set fix: 0 on a stream (next returns it while set) instead of replacing st.rand. The seed entries of the golden file gain one key, rng; their other keys stay unchanged.src/sim/rng.js (next, fromState, makeStream, drawer); the scenarios and tests/lib/tiny.js seed through a plain-data stream that gives mulberry32’s sequence, kept as st.rng.seed, and st.rand draws from it. Behaviour unchanged: the golden file differs only by the new key rng in its 16 scenario entries (re-recorded); npm run verify passes.st.rng.<system>; st.rand is gone; Culture, person() and the name generators get drawer(stream). Behaviour unchanged (golden: only rng changed). The stub states of tests/deaths.js, health.js and hunger.js carry a deaths stream (mulberry32) in place of their own generator (an LCG); their assertions hold.splitStreams(st, seed) at the end of each scenario’s seeding and in tests/lib/tiny.js gives each of the 12 systems makeStream(seed, name); tests/rng.js (in npm test) checks mulberry32’s sequence, JSON copies, fix, and that 1,000 extra draws on work leave births and deaths unchanged; checkState checks one unshared 32-bit stream per system and no fix left set.world/*, calibrate and apiKeys unchanged; the 4 */seed entries differ only in rng; every key of the 12 */year1..3 entries that depends on draws changed (pawns, households, herds, jobs, events, cenv, talks, weather and the rest), as intended.2e2e3d2) and after, tools/sim.js --map small:
```
populous small: population at years [1, 5, 10, 15]; totals of births, deaths, hunger deaths, marriages, hungry household-days
seed 3: before 354/398/449/500 b280 d137 h1 m100 hd558 after 353/384/434/476 b267 d147 h1 m97 hd383
seed 7: before 356/390/440/486 b286 d151 h2 m84 hd195 after 352/388/424/458 b275 d169 h4 m88 hd197
seed 11: before 318/347/400/437 b254 d135 h0 m84 hd145 after 322/365/405/455 b271 d135 h0 m85 hd215
seed 42: before 348/376/408/460 b237 d124 h1 m78 hd693 after 354/374/419/474 b242 d116 h2 m74 hd526founders small: population at years [1, 10, 20, 30]; totals of births, deaths, hunger deaths, marriages, hungry household-days seed 3: before 11/20/23/27 b27 d10 h0 m6 hd33 after 10/16/19/19 b21 d12 h0 m4 hd32 seed 7: before 10/16/20/25 b29 d14 h0 m6 hd86 after 12/21/26/26 b26 d10 h2 m4 hd233 seed 11: before 10/17/22/23 b29 d16 h2 m3 hd119 after 10/18/21/26 b30 d14 h1 m5 hd48 seed 42: before 10/17/25/23 b28 d15 h3 m4 hd134 after 10/20/14/15 b20 d15 h7 m6 hd45 ``` Founders at 8 more seeds (1, 2, 4, 5, 6, 8, 9, 10), year 30: before 16, 23, 27, 23, 22, 17, 20, 9 (sum 157; 7 hunger deaths; 675 hungry household-days), after 17, 23, 23, 18, 31, 17, 16, 20 (sum 165; 5; 614). Over all 12 founders seeds 255 before and 251 after; populous at year 15 1,883 before and 1,863 after (-1%), with fewer hungry household-days (1,591 to 1,321) and hunger deaths 4 to 7. The changes go both ways seed by seed and lie within the spread between seeds (founders 9 to 27 at year 30 before): a reshuffle, not a shift, for households as for totals. Founders at seed 42 falls to 15 (7 hunger deaths); a single founders village is fragile, which VB-163’s long-run test is meant to watch.