The [[Doom]] '''pseudorandom number generator''' is simplistic yet adequate for gameplay. Its simplicity has the virtue of speed.

The [[Doom]] '''pseudorandom number generator''' is simplistic yet adequate for gameplay. Its simplicity has the virtue of speed.

−

The file <tt>m_random.c</tt> 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 '''<code>P_Random</code>''' advances the index by one (wrapping around to zero after 255) and returns the table entry at that index.

+

The file <tt>m_random.c</tt> 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 '''<code>P_Random</code>''' advances the index by one (wrapping around to zero after 255) and returns the table entry at that index.

−

There is another function, '''<code>M_Random</code>''', that is identical except that it uses its own independent index. <code>P_Random</code> is used in play simulation situations, such as calculating hit damage. <code>M_Random</code> is used otherwise.

+

There is another function, '''<code>M_Random</code>''', that is identical except that it uses its own independent index. <code>P_Random</code> is used in play simulation situations, such as calculating hit damage. <code>M_Random</code> is used otherwise.

−

The reason for the existence of two individual indexes is to maintain [[multiplayer]] synchronisation: for example, <code>M_Random</code> 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 [[wikipedia:Model-view-controller|model-view-controller]] analogy, <code>P_Random</code> is used for random number generation at the 'model', while <code>M_Random</code> is used for random number generation at the 'view'.

+

The reason for the existence of two individual indexes is to maintain [[multiplayer]] synchronisation: for example, <code>M_Random</code> 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 [[wikipedia:Model-view-controller|model-view-controller]] analogy, <code>P_Random</code> is used for random number generation at the 'model', while <code>M_Random</code> is used for random number generation at the 'view'.

−

The function '''<code>M_ClearRandom</code>''' resets both functions' 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.

+

The function '''<code>M_ClearRandom</code>''' resets both functions' 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.

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.

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.

Latest revision as of 01:29, August 6, 2019

The Doompseudorandom 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 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 P_Random advances the index by one (wrapping around to zero after 255) and returns the table entry at that index.

There is another function, M_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 otherwise.

The reason for the existence of two individual indexes is to maintain multiplayer synchronisation: for example, M_Random 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 model-view-controller analogy, P_Random is used for random number generation at the 'model', while M_Random is used for random number generation at the 'view'.

The function M_ClearRandom resets both functions' 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.

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.