FUNDAMENTALS OF GAME DESIGN, SECOND EDITION
Random Numbers and the Gaussian Curve
So many games use random numbers that, although you may not know any programming, you should understand how to use random numbers in a computer game.
When a computer generates random numbers, by convention it always does so as a real number value greater than or equal to 0 but less than 1. In statistical calculations, probabilities are always expressed as a fractional value between 0 and 1, so an event with a probability of 0.1 has 1 chance in 10, or 10 percent, of occurring.
To see if an event with a given probability occurs, the computer generates a random number, then checks to see if the random number is less than the event's probability. If so, the event happens. The random number is always less than 1, so if an event's probability is exactly 1, the event always happens. The random number is never less than 0, so if an event's probability is 0, it never happens.
The "Numeric Attributes" section of Chapter 14 discusses a number of ways to use random numbers when computing things such as whether a weapon hits the point at which it's aimed. A weapon with an accuracy rating of 0.8 hits its mark 80 percent of the time. To see whether a particular shot hits, generate a random number and compare the number to the weapon's accuracy rating. If the random number is less than the rating, the weapon hits.
Random-number generation algorithms normally take an input value, called a seed, that determines the sequence of random numbers the algorithm produces. If the seed is identical each time the game is played, the sequence of random numbers that the algorithm generates is identical each time, too. In other words, it's as if each time you play a board game, you get the exact same sequence of die rolls that you got the last time you played. Each roll is different from the previous roll, but the sequence of rolls is identical. Such numbers are called pseudo-random.
This feature is extremely useful when you're tuning the game's mechanics. When you make adjustments to the mechanics, it is difficult to determine what the effect of your change is if the operation of chance keeps changing the game. By using the same seed each time you play, you always get the same random numbers, so the effects of chance don't change from one playing to the next. The mechanics become deterministic and predictable. This quality is also essential for bug-fixing.
If a bug happens by chance, it might not happen the next time someone plays the game, so the programmer won't be able to find it and fix it. If the game uses pseudo-random numbers, the bug should be easier to reproduce.
Naturally, in the final version of the game that the customer buys, you won't want the effects of chance to be the same on each play through. Just before the game is ready to ship, the programmers will change the code to take the seed from some random source, such as the system clock, so the player will get a different experience each time he plays.