Sub topic 16
Seeds, Replays, and Reproducible Bugs
Assume you know basic probability, common game mechanics, and how to read simple Python code. This chapter focuses on a practical question: how can you make a random event happen the same way again?
What a seed does
A pseudo-random number generator, or PRNG, is an algorithm that produces a sequence of numbers that looks random. It is not truly random because the sequence comes from an internal starting value called a seed.
If two PRNGs use the same algorithm and the same seed, they produce the same sequence:
import random
first = random.Random(123)
second = random.Random(123)
sequence_a = [first.randint(1, 6) for _ in range(5)]
sequence_b = [second.randint(1, 6) for _ in range(5)]
print(sequence_a)
print(sequence_b)
print(sequence_a == sequence_b)The final line prints True. This property is called determinism. It means that the same starting conditions produce the same results.
A seed is useful for:
- Replaying a match for debugging
- Sharing a challenge with the same level generation
- Reproducing a player report
- Comparing two balance changes under identical random events
- Running automated tests with predictable outcomes
A seed does not record player decisions. It only sets the starting point of the random sequence.
A replay needs inputs and randomness
A replay usually needs both the random seed and the player’s actions. If a player takes a different action, the game may consume a different number of random values. Every later random result can then change.
This example records the actions and uses a local generator. A local generator is safer than Python’s global random generator because unrelated code cannot accidentally change its sequence.
import random
def play_match(seed, actions):
rng = random.Random(seed)
score = 0
events = []
for action in actions:
roll = rng.randint(1, 6)
if action == "attack":
points = roll
elif action == "defend":
points = roll // 2
else:
raise ValueError(f"Unknown action: {action}")
score += points
events.append({
"action": action,
"roll": roll,
"points": points,
})
return score, events
seed = 42
actions = ["attack", "defend", "attack"]
score, events = play_match(seed, actions)
same_score, same_events = play_match(seed, actions)
print(score)
print(events == same_events)The second run produces the same events because it has the same seed and the same action sequence.
Seeds, states, and replay data
| Method | Stores | Best use | Main limitation |
|---|---|---|---|
| Seed replay | Seed and player inputs | Replaying a complete match | Breaks if random-call order changes |
| State snapshot | Full PRNG state at a point | Debugging from the middle of a match | Larger and less portable |
| Event replay | Recorded outcomes and inputs | Exact playback across code changes | Requires more storage |
A PRNG state is the generator’s current internal position. Saving the state lets you continue from a particular moment, even after many random values have been used. In Python, getstate() and setstate() provide this ability.
Reproducing a bug
When a player reports a random bug, collect:
- The game version and platform
- The random seed, if available
- The player’s actions or input log
- The relevant game state
- The exact observed result
Then run the same scenario locally. Check whether the result is deterministic. If it is, add the scenario to a regression test so future changes cannot silently reintroduce the bug.
A seed replay can fail after a code change if the game adds a new random call, changes update order, or lets unrelated systems share one generator. Separate generators for separate systems, such as combat and map generation, reduce this problem.
Exercise
A replay uses the same seed but produces a different result after a patch. Name two likely causes.
Show answer
The patch may have changed the order or number of random calls, or the replay may be missing an input or piece of game state.