Pseudorandom number generator

The pseudorandom number generator used by the Doom engine is simplistic yet adequate for gameplay. Its simplicity has the virtue of speed.

Implementation
The file in the Doom source code contains a static table 256 bytes long containing numbers between 0 and 255 in a fixed, scrambled order. There is an index to this table which starts at zero. Each call to the function advances the index by one, wrapping around to zero after 255, and returns the table entry at that index.

There is another function,, that is identical except that it uses its own independent index. is used in play simulation situations, such as calculating hit damage. is used otherwise.

The reason for the existence of two individual indexes is to maintain multiplayer synchronisation: for example, is used to apply a random pitch variation to sounds. As two players may not hear the same sound effect (they may be in different parts of the level), using a single index would cause the game to become desynchronised. To use a analogy,  is used for random number generation at the 'model', while  is used for random number generation at the 'view'.

The function resets both functions' indexes to zero. It is called during initialization of each new game so that demos will be the same each time they are played, and so that multiplayer games are synchronised.

Although the table is 256 bytes long, it does not contain all of the numbers between 0 and 255 inclusive. For example, 0 appears twice and 1 does not appear at all; 145 appears five times, more than any other number. Thus the values are not uniformly distributed, but in fact they are nearly so. The mean value is 128.852, whereas it would be 127.500 if all values were equally likely. All of this suggests that the table was generated using a conventional pseudorandom number generator of reasonable quality.

History
The same table of random numbers used by Doom is also found in the The Catacomb,, Catacomb 3D , Keen Dreams, Commander Keen 4-6, Wolfenstein 3D, In Pursuit of Greed and Duke Nukem II code bases. An accompanying array of Fibonacci series numbers suggests that the sequence might have been serialized output of an earlier. Similar code can be found in the original source, but the table itself is nowhere to be found, as it is generated dynamically at runtime in that game engine.