Game Development Reference
In-Depth Information
Dealing with Randomness in Games
One of the most important parts of the paint-can behavior is that some aspects of it should be
unpredictable . You don't want every can falling at a predictable speed or time. You want to add a
factor of randomness so that every time the player starts a new game, the game will be different.
Of course, you also need to keep this randomness in control. You don't want one can to take three
hours to fall from top to bottom while another can takes only one millisecond. The speed should be
random, but within a playable range of speeds .
What does randomness actually mean? Generally, random events or values in games and other
applications are managed by a random-number generator . In JavaScript, there is a random method
that is part of the Math object. You might wonder: how does a computer generate a completely
random number? Does randomness exist in reality? Isn't randomness just a manifestation of
behavior that you can't yet fully predict and therefore call “random”? Well, let's not get too
philosophical. In game worlds and computer programs, you can predict precisely what is going to
happen, because a computer can only do exactly what you tell it to do. Therefore, strictly speaking,
a computer isn't capable of producing a completely random number. One way to pretend that
you can produce random numbers is by picking a number from a predefined, very large table of
numbers. Because you aren't really producing random numbers, this is called a pseudo-random
number generator . Most random-number generators can generate a number in a range, such
as between 0 or 1, but they often can also generate an arbitrary number or a number in another
range. Every number within the range has an equal chance of being generated. In statistics, such a
distribution is called a uniform distribution .
Suppose that when you start a game, you begin generating “random” numbers by walking through
the table. Because the number table doesn't change, every time you play the game, the same
sequence of random numbers is generated. In order to avoid this problem, you can indicate in the
beginning that you want to start at a different position in the table. The position where you start in
the table is also called the seed of the random-number generator. Often, you take a value for the
seed that is different every time you start the program, such as the current system time.
How do you use a random-number generator to create randomness in your game world? Suppose
you want to spawn an enemy 75% of the times that a user steps through a door. In that case, you
generate a random number between 0 and 1. If the number is less than or equal to 0.75, you spawn
an enemy; otherwise you don't. Because of the uniform distribution, this will lead exactly to the
behavior that you require. The following JavaScript code illustrates this:
var spawnEnemyProbability = Math.random();
if (spawnEnemyProbability >=0.75)
// spawn an enemy
else
// do something else
If you want to calculate a random speed between 0.5 and 1, you generate a random number
between 0 and 1, divide this number by 2, and add 0.5:
var newSpeed = Math.random()/2 * 0.5;
 
Search WWH ::




Custom Search