Chance in Video Games

Pseudo-Random Number Generators in Games Explained

How seeded algorithms produce number sequences that look random, with a tiny worked example, plus why games rely on them and how modulo can skew results.

On this page
  1. Randomness from a formula
  2. A tiny example you can check by hand
  3. Why games use them
  4. Turning raw numbers into outcomes
  5. Seeds and where they come from
  6. Pseudo-random versus truly random
  7. What a PRNG does not do

Randomness from a formula

A computer follows instructions, so it cannot produce unpredictability from nothing. What it can do is run a formula that turns one number into the next in a way that looks random. This is a pseudo-random number generator, or PRNG. Each value is calculated from the previous internal state, so the whole sequence is fixed once the starting point is chosen. That starting point is the seed.

Two things follow directly. The same seed always gives the same sequence, and a good generator’s sequence passes many tests for looking unpredictable, so a player cannot spot a pattern by eye.

A tiny example you can check by hand

A classic type is the linear congruential generator. It repeats one step: multiply the current number, add a constant, and keep the remainder after dividing by a fixed modulus. Real generators use enormous numbers; this toy uses small ones so you can follow every step.

Rule: next = (5 × current + 3) mod 16, with seed 7.

Step Calculation Result
1 5 × 7 + 3 = 38, remainder after dividing by 16 6
2 5 × 6 + 3 = 33 1
3 5 × 1 + 3 = 8 8
4 5 × 8 + 3 = 43 11
5 5 × 11 + 3 = 58 10
6 5 × 10 + 3 = 53 5

Continuing the same way gives 12, 15, 14, 9, 0, 3, 2, 13, 4 and then 7, which is the seed again. After exactly 16 steps the sequence repeats from the start, having visited every value from 0 to 15 once. The numbers 6, 1, 8, 11, 10, 5 look scattered, yet they follow a strict rule.

Two lessons come from this toy:

  • Every generator repeats eventually. The length of the cycle is called its period. This toy has a period of 16, which would be useless in a game. Real generators have periods so long that repetition is not a practical concern.
  • The state is the number. Anyone who knows the current value and the rule can predict every later number. That is fine for a game’s dice but a problem for anything where predictability could be exploited.

Why games use them

Games choose pseudo-random generators because of their properties, not in spite of them:

  • Speed. Generating a value takes very little time, which matters when thousands of rolls may happen every frame.
  • Reproducibility. Replays, bug reports and saved worlds depend on being able to rebuild a sequence from a seed.
  • Synchronisation. In some multiplayer designs, each player’s machine runs the same generator from the same seed so that both compute identical results without sending every roll across the network.
  • Control. Developers can reseed on purpose, for example per level, to get stable behaviour for testing.

Turning raw numbers into outcomes

The generator gives a number in a big range. The game then converts it into the range it needs. A common way is the modulo operation: take the remainder after dividing. To roll a six-sided die from a number in 0 to 15, you could compute the number mod 6 and add 1.

That reveals a subtle issue called modulo bias. The values 0 to 15 are 16 values, and 16 does not divide evenly by 6:

  • Remainders 0, 1, 2 and 3 each come from three values (for example, 0, 6 and 12 all give remainder 0).
  • Remainders 4 and 5 each come from only two values (4 and 10, or 5 and 11).

So four faces would each have a 3 in 16 chance (18.75%) and two faces would have a 2 in 16 chance (12.5%), instead of 1 in 6 (about 16.7%) each. A real generator’s range is vastly larger, so the skew is tiny, but careful programmers still discard values that fall in the uneven leftover section. The same principle applies to any mapping of raw numbers onto outcomes, including the weighted tables used for loot. Whether the dice in a given game are fair in practice is a question of implementation, much as with physical dice fairness.

Seeds and where they come from

If a game always started from the same seed, every playthrough would be identical. To avoid that, many games seed from something that changes, such as the system clock or a value gathered from the operating system. Others let the player enter a seed so that a world can be shared.

How a game treats the generator around saving and loading matters a great deal for how play feels:

  • If the generator state is saved with the game, reloading and repeating an action tends to give the same result.
  • If it is reseeded on load, the same action may give a different result.
  • If a game draws numbers for animation or effects from the same generator as for combat, the sequence of results can shift depending on things the player did not expect, such as how long they watched a cutscene.

These design choices are rarely documented, which is why players sometimes believe a game “remembers” or “punishes” a failed attempt when the explanation is the generator state.

Pseudo-random versus truly random

True randomness comes from physical processes, such as electrical noise, and some systems mix those into their seeds. Games usually do not need that. Where outcomes carry money or security consequences, generators are typically held to stricter standards and are often tested, because a predictable sequence could be exploited. For ordinary game rolls, quality matters mainly in avoiding visible patterns and bias.

What a PRNG does not do

A generator on its own does not make results feel fair. It produces sequences with natural streaks, and a plain independent roll will still hand a player long runs of misses. Whether the game adds adjustments on top, such as the shuffle-style systems described in the article on streak breakers, is a separate design decision. And because the sequence is deterministic, properties like a stated chance of 25% only describe the long-run share of outcomes. In any short stretch the share can differ a good deal, which is where random chance often surprises people.

The complete guide to chance in video games

How Randomness Works in Video Games
Read the guide

Keep exploring

Guides from other topics

All topics →