TExcelTAD
Home › Math for Games › Seeded Random Numbers

Seeded Random Numbers

Computers don't produce truly random numbers in games; they use pseudo-random number generators (PRNGs): small functions that turn a seed into a long sequence of numbers that look random. The same seed always gives the same sequence. That property is not a weakness, it is a feature you will rely on constantly:

  • Replays and multiplayer lockstep: every machine simulates the same "random" events.
  • Procedural worlds: Minecraft-style seeds rebuild an entire world from one number.
  • Debugging: a bug that happens "randomly" happens every time with the same seed.
  • Daily challenges: seed with the date and every player gets the same run.

A tiny generator: PCG32

PCG is a modern, fast, high-quality family of generators. The 32-bit version is a few lines: advance a 64-bit state with a multiply and add, then scramble it into 32 output bits.

cpp · try it yourself
#include <iostream>
#include <cstdint>

struct Pcg32 {
    uint64_t state, inc;
    Pcg32(uint64_t seed, uint64_t stream = 54) : state(0), inc((stream << 1) | 1) {
        next(); state += seed; next();
    }
    uint32_t next() {
        uint64_t old = state;
        state = old * 6364136223846793005ULL + inc;
        uint32_t xorshifted = uint32_t(((old >> 18) ^ old) >> 27);
        uint32_t rot = uint32_t(old >> 59);
        return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
    }
    double nextDouble() { return next() / 4294967296.0; }                 // [0, 1)
    int range(int lo, int hi) { return lo + int(nextDouble() * (hi - lo + 1)); }   // [lo, hi]
};

int main() {
    Pcg32 a(42), b(42), c(7);
    std::cout << "seed 42:";
    for (int i = 0; i < 5; i++) std::cout << " " << a.range(1, 6);
    std::cout << "\nseed 42:";
    for (int i = 0; i < 5; i++) std::cout << " " << b.range(1, 6);
    std::cout << "\nseed 7: ";
    for (int i = 0; i < 5; i++) std::cout << " " << c.range(1, 6);
    std::cout << "\n";
}
seed 42: 4 3 5 4 5
seed 42: 4 3 5 4 5
seed 7:  4 3 1 5 5

The two generators with seed 42 roll identical dice; seed 7 rolls differently. Because this code uses only integer arithmetic with exact, specified results, it prints the same numbers on every compiler and platform.

What about the standard library?

C++'s <random> engines such as std::mt19937 are fully specified, so their raw output is identical everywhere. The distributions are not: std::uniform_int_distribution and std::uniform_real_distribution may produce different values on GCC, Clang and MSVC for the same engine state. For anything that must match across platforms (lockstep multiplayer, shared seeds), map the raw numbers yourself.

cpp · try it yourself
#include <iostream>
#include <random>

int main() {
    std::mt19937 engine(2024);
    std::cout << "raw mt19937 (portable):";
    for (int i = 0; i < 3; i++) std::cout << " " << engine();
    std::cout << "\n";

    engine.seed(2024);
    std::uniform_int_distribution<int> d6(1, 6);         // fine for single-platform code
    std::cout << "d6 via distribution (implementation-specific):";
    for (int i = 0; i < 5; i++) std::cout << " " << d6(engine);
    std::cout << "\n";
}
raw mt19937 (portable): 2525503112 3251949050 3002649184
d6 via distribution (implementation-specific): 4 5 5 5 2

The first line is guaranteed by the C++ standard; the second is what this compiler's library happens to produce.

Note: The same caution applies to engines: Unity's UnityEngine.Random and .NET's System.Random are deterministic for a given seed only within the same runtime version. Unity.Mathematics' Unity.Mathematics.Random (xorshift-based) is a deterministic struct you can store per system, which is why DOTS code uses it.

One generator per purpose

A classic bug: the level generator and the loot system share one global generator. Add one extra loot roll and the whole level changes, because every later number shifted. Give each system its own generator, seeded from the world seed plus a constant, so they can't disturb each other:

cpp · try it yourself
#include <iostream>
#include <cstdint>

// SplitMix64: a tiny generator that is also the standard way to derive seeds.
struct SplitMix64 {
    uint64_t s;
    explicit SplitMix64(uint64_t seed) : s(seed) {}
    uint64_t next() {
        uint64_t z = (s += 0x9E3779B97F4A7C15ULL);
        z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
        z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
        return z ^ (z >> 31);
    }
    int range(int lo, int hi) { return lo + int((next() >> 11) * (1.0 / 9007199254740992.0) * (hi - lo + 1)); }
};

int main() {
    const uint64_t worldSeed = 123456;
    for (int extraLootRolls : {0, 3}) {
        SplitMix64 terrain(worldSeed ^ 0x7465727261696EULL);   // "terrain"
        SplitMix64 loot(worldSeed ^ 0x6C6F6F74ULL);            // "loot"
        for (int i = 0; i < extraLootRolls; i++) loot.next();  // the loot code changed...
        std::cout << "extra loot rolls " << extraLootRolls << ": terrain heights";
        for (int i = 0; i < 6; i++) std::cout << " " << terrain.range(0, 9);
        std::cout << " | loot " << loot.range(1, 100) << "\n";
    }
}
extra loot rolls 0: terrain heights 7 0 9 2 8 4 | loot 21
extra loot rolls 3: terrain heights 7 0 9 2 8 4 | loot 59

The terrain is identical in both runs; only the loot changed. Stateless hashing goes one step further: instead of a sequence, compute hash(seed, x, y) for each tile. Any tile can then be generated in any order, which is how infinite worlds load chunks on demand (the Noise lesson uses this).

Test yourself

Why do games prefer seeded pseudo-random generators?

  1. They are truly random
  2. The same seed reproduces the same sequence, enabling replays, shared worlds and debugging
  3. They are slower and therefore safer
  4. They never repeat a number

Which part of C++ <random> is NOT guaranteed to give the same values across standard libraries?

  1. std::mt19937's raw output
  2. std::uniform_int_distribution
  3. The seed value
  4. std::minstd_rand's raw output

Why give terrain generation and loot their own generators?

  1. To use more memory
  2. So changing how many numbers one system draws doesn't change the other's results
  3. Because one generator can only produce 100 numbers
  4. It is required by C++