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

In m_random.c there is 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 that starts at zero. Each call to the function M_Random() advances the index by one (wrapping around to zero after 255) and returns the table entry at that index.

There is another function, P_Random() that is identical except that it uses its own independent index. P_Random is used in play simulation situations, such as calculating hit damage. M_Random is used everywhere else.

The function M_ClearRandom() resets both function's indexes to zero. It is called during initialization of each new level, primarily so that a demo will be the same each time it is played.

See also: Wikipedia:Pseudorandom number generator
Community content is available under CC-BY-SA unless otherwise noted.