skip to content

Players of a roguelike share a run seed, yet the same seed gives different loot on some machines; how do you find and fix the divergence?

level: seniorimportance: should knowfreq 36%

answer

  1. same seed, same calls, same order
  2. who else draws from the generator
  3. one stream per system
  4. hash the seed per level
  5. log draws, diff the first mismatch

basics

~20 s

A seed replays only when every gameplay draw happens in the same order with the same algorithm. Log draws on both machines and diff for the first mismatch, then give gameplay its own codebase-owned generator streams that cosmetic, frame-rate or thread-order draws never touch.

solid answer

~50 s

First find the first divergent draw: in a debug build, log `(stream, call site, draw index, value)` on both machines and diff. The usual culprits are a shared generator that particles, camera shake or UI also draw from (so graphics settings shift every later loot roll), draws made per rendered frame rather than per gameplay event, iteration over an unordered container or across worker threads, and a platform's built-in random function whose algorithm differs between platforms or library versions. The fix is structural: implement a small, known generator in the codebase (a PCG or xorshift-family generator), give each system its own stream (world, loot, combat, cosmetics), derive each level's seed by hashing the run seed with the level index so later levels do not depend on earlier draws, save stream state in saves, and keep range mapping in integers.

code

pseudocode · 15 lines
pseudocode
// xorshift32: never seed with 0
function next_u32(state):
    x = state.value
    x = x xor (x << 13)
    x = x xor (x >> 17)                 // logical shift on an unsigned 32-bit value
    x = x xor (x << 5)
    state.value = x                     // all arithmetic wraps at 32 bits
    return x

function stream_seed(run_seed, stream_name, index):
    h = mix64(run_seed xor hash64(stream_name))
    h = mix64(h xor index)
    s = low32(h)
    if s == 0: s = 1                    // the zero state would emit zeros forever
    return s

go deeper

for a junior

Recall that a seed fixes a sequence of numbers, not a game: the replay holds only if the same draws happen in the same order.

for a middle

Explain the generator choices (LCG low-bit weakness, xorshift's zero state, PCG's permuted output and streams) and why cosmetics must not share a gameplay stream.

for a senior

Diagnose with a per-draw log diff to the first mismatch, then fix structurally with owned generators, per-system streams, per-level derived seeds and stable iteration order.

for a principal

Decide the seed contract: which systems are seed-stable, how seeds are versioned against content patches, and the replay tests that guard it in the build.

## What a seed actually guarantees A **pseudo-random number generator (PRNG)** is a deterministic function: a small **state**, an update step that advances it, and an output function. A **seed** sets the initial state. The same seed produces the same sequence of numbers, and nothing more. A roguelike that lets players share a run seed is promising something stronger: the same **game**. That holds only if every system asks for the same numbers, in the same order, through the same algorithm, and turns them into the same results. So a divergence report ("same seed, different first chest on the other laptop") is almost never a broken generator. It is a difference in **who drew how many numbers, in what order**. ## Choosing the generator Use an algorithm you implement and control rather than a platform's built-in random function, whose algorithm, state size and range mapping are often unspecified and can differ between platforms, compilers or library versions. | Generator | State | Period | Quality and traps | |---|---|---|---| | Linear congruential (LCG), power-of-two modulus | one word | up to `2^k` for a `k`-bit state | fast; low bits are weak: the lowest bit alternates, so use the high bits | | xorshift (32-bit, shifts 13, 17, 5) | one 32-bit word | `2^32 - 1` | a few shifts and XORs; a zero state stays zero forever; plain variants fail some linearity tests, scrambled variants fix that | | PCG (for example 64-bit state, 32-bit output) | one 64-bit word plus an increment | `2^64` | an LCG step plus an output permutation of the high bits; an odd increment selects one of many distinct streams; strong statistical quality | | Mersenne Twister | about 2.5 KB | `2^19937 - 1` | good statistics but a large state that is costly to copy into every save and every stream | An enormous period does not fix desyncs: a replay drifts because of call order, not because the sequence repeats. ## Where replays diverge - **One generator shared with cosmetics.** Particles, camera shake, ambient barks and UI flourishes draw from the same stream as loot. A lower particle setting or a different screen size consumes fewer draws, and every later loot roll shifts. - **Draws tied to rendered frames.** A system that rolls "a chance per frame" draws more often at 144 frames per second than at 30. Roll per gameplay event or per fixed simulation step. - **Unstable iteration order.** Assigning drops while iterating an unordered hash container, or rolling from several worker threads that share a stream, changes order between runs and machines. Sort by a stable id, or give each job its own derived stream. - **Seed taken from the clock** somewhere in a code path, often a fallback when the seed field is empty. - **Range mapping and floats.** Mapping to a range through floating-point multiplication can round differently across platforms; floating-point non-determinism is its own topic, but keeping range mapping in integers removes it from the generator. - **Content changes.** A patch that adds a loot entry changes what the same draw selects. Version the seed format with the game build and say so in the seed-sharing UI. ## Streams and derived seeds The robust structure is several independent generators, each owned by one system: 1. Hash the run seed with a stream name and an index to get each stream's seed: `seed_for(run_seed, "loot", level_index)`. A good 64-bit mixing function turns nearby inputs into unrelated outputs. 2. Give world generation, loot, combat, enemy decisions and cosmetics their own streams. Cosmetics can then draw freely without moving anything that matters. 3. Derive **per level** rather than continuing one stream across the run, so level 5 is identical however many draws levels 1 to 4 consumed, and a player who rerolls a shop does not change the next floor. 4. Store each stream's state in the save file, which is cheap for a one-word generator. 5. Never pass a gameplay stream to a function whose draw count depends on something outside the simulation, such as a rejection loop fed by visual data. ## Finding the first divergent draw 1. Add a debug option that logs every gameplay draw as `(stream, call site, draw index, value)`. 2. Reproduce on both machines with the same seed and the same inputs. 3. Diff the logs and look at the **first** mismatch; everything after it is fallout. 4. The call site before the mismatch shows which system drew an extra number or drew out of order. 5. Keep a cheap per-stream checksum of draw counts in automated replay tests, so a regression fails the build rather than reaching players.

  • Why does a power-of-two-modulus LCG make bad coin flips with next() mod 2?
    With an odd multiplier and an odd increment, which a full-period power-of-two LCG has, the lowest bit flips every step, so `next() mod 2` strictly alternates. Each low bit `j` cycles with period `2^(j + 1)`. Take the high bits instead, or use a generator such as PCG that permutes its output.
  • How do you keep replays reproducible when work is spread across worker threads?
    Never let threads share one stream, because draw order then follows scheduling. Derive a stream per job from the step's seed and a stable job id, such as an entity id or chunk coordinate, and merge results in a stable order. The outcome no longer depends on which thread ran first.
  • Is a cryptographically secure generator a fix for replay divergence?
    No. Divergence comes from call order and call count, which any deterministic generator inherits. A secure generator seeded from system entropy cannot replay at all, and a seeded one is slower with the same ordering problem. Secure randomness belongs to protecting secrets, not to reproducible gameplay.

  • Same seed means same game, whatever else draws from the generator.
  • A longer-period generator will stop the replays from drifting.
  • The platform's built-in random function is identical on every platform.
  • Cosmetic effects can safely share the loot generator because nobody notices them.
  • Rolling a chance once per rendered frame is deterministic for a fixed seed.