Simulating Chance

Pseudo-Random Number Generators Explained

How computers produce deterministic but random-looking numbers: what a seed is, what a period means, and a tiny hand-checked generator that shows the idea.

On this page
  1. Random-looking, but fully determined
  2. Seeds
  3. A tiny generator you can check by hand
  4. Periods
  5. What a good generator is trying to do
  6. Limits and cautions
  7. Why this matters for games and gambling topics
  8. Using this in your own simulations

Random-looking, but fully determined

A computer follows instructions, so it cannot simply “be random”. What most programs use instead is a pseudo-random number generator: a procedure that takes some internal state, produces a number from it, and updates the state to produce the next number. The output looks random for practical purposes, but if you know the starting state, every number that follows is completely determined.

This sounds like a flaw, but for simulations it is usually an advantage. It means you can repeat a run exactly, which makes bugs easier to find and results easier to share.

Seeds

The starting state is set by a seed. Give a generator the same seed and it produces the same sequence every time. In Python, for example, calling random.seed(42) and then drawing numbers will give the same list on every run of the program, on any machine using the same version of the library.

If you do not set a seed, most languages choose one for you from something unpredictable, such as the system clock or operating system entropy. That is why a simulation that never mentions a seed gives different answers each run.

Both choices are useful:

  • Fixed seed: for testing, debugging and reproducible examples.
  • Varying seed: for ordinary runs where you want fresh numbers each time.

Games use the same idea. Procedural generation with seeded randomness lets a game rebuild the same world from a short code, because the same seed regenerates the same sequence of “random” choices.

A tiny generator you can check by hand

One of the oldest designs is the linear congruential generator. It updates the state with a simple rule:

next = (a x current + c) mod m

Here is a deliberately small example with a = 5, c = 3 and m = 16, starting from the seed 7. Each step multiplies by 5, adds 3, and keeps the remainder after dividing by 16.

  • 5 x 7 + 3 = 38, and 38 mod 16 = 6
  • 5 x 6 + 3 = 33, and 33 mod 16 = 1
  • 5 x 1 + 3 = 8
  • 5 x 8 + 3 = 43, and 43 mod 16 = 11
  • 5 x 11 + 3 = 58, and 58 mod 16 = 10

The sequence begins 7, 6, 1, 8, 11, 10, and carries on. Continuing the same arithmetic, it visits 5, 12, 15, 14, 9, 0, 3, 2, 13 and 4, and then the next value is 7 again. That is all 16 possible values, each appearing once, before the pattern repeats.

This toy generator is far too small and regular for real use, but it shows the essentials: a rule, a seed, and a sequence that eventually cycles.

Periods

The number of values a generator produces before the sequence repeats is called its period. In the example above the period is 16. A generator can never have a period longer than its number of possible internal states, and a poorly chosen rule can have a much shorter one.

Real generators have enormous periods. The Mersenne Twister, which Python’s standard random module has long used, has a period of 2^19937 - 1, a number with thousands of digits. For simulation purposes, you will never come close to exhausting it.

A long period alone is not the whole story. Quality also depends on whether the numbers are spread evenly, whether successive values show hidden patterns, and whether combinations of consecutive outputs behave as independent draws should. Statisticians run batteries of tests on generators to look for such weaknesses.

What a good generator is trying to do

For simulation work, a good generator should produce numbers that, for practical purposes, behave like independent draws from a uniform distribution. Each possible value should be about equally likely, and knowing past outputs should not usefully predict the next one in ordinary statistical tests.

From uniform numbers you can build everything else. A die roll comes from mapping the uniform range onto six values. A coin toss uses a threshold at 0.5. A weighted choice uses a threshold matching the weights. The generator is the foundation under all these.

Limits and cautions

  • Not for security. Generators designed for simulation are generally not designed to be unpredictable to an adversary. Passwords, keys and tokens need a cryptographically secure source, which software libraries provide separately.
  • Seeds limit reach. A generator started from a small seed can only produce as many different sequences as there are seeds. This matters for shuffles: a deck has 52! orderings, about 8 x 10^67, far more than a seed of 32 bits can select.
  • Careless use can hurt. Reseeding a generator repeatedly with the same value, for example inside a loop, makes every draw identical. Seed once, at the start.
  • Reproducibility depends on versions. The same seed may give different sequences in different libraries or versions.

Why this matters for games and gambling topics

Computer games and online games of chance rely on random number generators, and the details of how they are built and checked matter to whether outcomes are fair. This site does not assess any particular product, and nothing here says how a given game is built. The general point is simple: a sound generator makes outcomes unpredictable and independent, so past results do not change future odds. Pattern-spotting in a sequence of outputs is not a method that works, and the gambler’s fallacy remains a fallacy.

Casino games carry a house edge regardless of how the numbers are produced, and gambling can be harmful. Anyone struggling can contact a local support service.

Using this in your own simulations

For nearly all educational simulations, the generator built into your language is perfectly adequate. Set a seed when you want reproducibility, leave it unset when you do not, and report the number of trials alongside your results. If a result looks suspicious, try a different seed and see whether it holds.

The complete guide to simulating chance

Simulating Probability with Simple Code
Read the guide

Keep exploring

Guides from other topics

All topics →