LockerGnomeTech news and nostalgia for today's technology professionals
Subscribe

Search

Gaming

Doom keeps its random numbers in a fixed 256-byte loop

Doom’s released source reads successive values from a fixed table. Separate indexes walk the same 256 entries and wrap around.

Chris Pirillo turns the crank of a praxinoscope drum whose looping strip shows the same ring of dice again and again
Illustration: LockerGnome

Doom can surprise you while consulting the same tiny list again and again. The file m_random.c declares “unsigned char rndtable[256]”, followed by two functions that fetch from it.

P_Random increments prndindex; M_Random increments rndindex. Each masks the new index with 0xff, so it stays in the 0–255 range, then returns the byte at that position. M_ClearRandom sets both indexes back to zero.

The separate indexes matter. A call to one function advances its own place without spending an entry from the other function’s sequence. The source makes that separation visible without needing a theory about everything else the game does.

The next useful file is whichever caller spends one of those numbers. That is where a tiny fixed list starts turning into a monster’s decision.

More Gaming

Every Gaming story →