Sub topic 15
Pseudo-Random Number Generators
This chapter assumes basic probability, familiarity with dice and card mechanics, and the ability to read simple Python. You will learn how games produce repeatable-looking randomness and how to control it during testing.
What a PRNG does
A pseudo-random number generator (PRNG) is an algorithm that produces a sequence of numbers that appears random. “Pseudo” means the sequence is calculated rather than created from a truly unpredictable physical process.
A PRNG has an internal state. Each generated value changes that state, and the next value depends on the new state:
- Start with an initial state.
- Apply a mathematical function.
- Return a value.
- Store the changed state for the next request.
If two generators start with the same state and receive the same requests, they produce the same sequence. To a player who does not know the state, the sequence can still look random.
import random
rng = random.Random(2026)
rolls = [rng.randint(1, 6) for _ in range(10)]
print(rolls)random.Random(2026) creates a separate generator with a seed of 2026. A seed is the value used to initialize the generator's state. Running this program again produces the same ten rolls.
Use a separate generator instead of the module-wide random functions when you want clear ownership. Combat, map generation, and visual effects can each have their own generator. A change in one system then has less chance of changing random results in another system.
Why deterministic randomness helps
Determinism means that the same inputs produce the same outputs. It is useful for:
- Replaying a match from its starting seed and player actions.
- Reproducing a bug reported by a player.
- Comparing two versions of a game system.
- Running automated simulations with controlled conditions.
- Generating the same world for every player who uses a shared seed.
A seed does not by itself guarantee a replay. The game must also make the same random requests in the same order. An extra random call for a particle effect can shift every later result. Keep cosmetic randomness separate from gameplay randomness, or record the important random events directly.
Common errors and design choices
A PRNG is not a guarantee of fairness. It only supplies values according to its algorithm. Your selection method determines the final distribution.
For example, choosing an integer with randint(1, 6) gives each die face an equal chance in Python's standard generator. Taking a large random integer and applying a remainder operation such as value % 6 can create modulo bias when the source range is not evenly divisible by six.
| Need | Suitable approach | Main concern |
|---|---|---|
| Ordinary game rolls | A well-tested PRNG | Streaks still occur |
| Replays and tests | A seeded, owned generator | Request order must match |
| Secure tokens or prizes with monetary value | A cryptographic generator | Use the platform's security APIs |
| Controlled pacing | Shuffle bag or pity system | It is no longer independent chance |
A game PRNG is usually appropriate for dice, enemy behavior, and procedural content. It is not appropriate for passwords, authentication tokens, or systems where players could profit by predicting outcomes. Standard game generators are designed for speed and statistical quality, not secrecy.
> callout tip > Do not “fix” an unlucky result by changing the seed after each roll. That makes behavior harder to test and can create hidden bias. Adjust the game mechanic deliberately with a shuffle bag, pity timer, or other visible rule.
Exercise
Exercise
Why might a replay fail even when it uses the same seed?
Show answer
The game may make random requests in a different order, such as from a new visual effect or a changed gameplay branch. The same seed only reproduces the sequence when the requests and inputs also match.