Pseudorandom number generator

The Doom Pseudorandom number generator is simplistic yet adequate for gameplay. Its simplicity has the virtue of speed.

The file m_random.c in the Doom source code contains a static table 256 bytes long containing the numbers from 0 to 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 everywhere else.

The reason for the existence of two individual indexes is to maintain demo and 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 an MVC (Model-View-Controller) analogy,  is used for random number generation at the 'model', while   is used for random number generation at the 'view'.

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


 * See also: Pseudorandom number generator