Game Development Reference
In-Depth Information
we actually
can
predict precisely what is going to happen, because a computer can
only do exactly what we tell it to do. Therefore, strictly speaking, a computer is not
capable of producing a completely random number. One way to pretend that we can
produce random numbers is by picking a number from a predefined, very large table
of numbers. Because we are not really producing random numbers, this is called a
pseudo-random number generator
. Most random number generators can generate
a number between a range, such as 0 or 1, but they often can also generate an ar-
bitrary number or a number between another range. Each number within the range
has an equal chance of being generated as any other number. In statistics, such a
distribution is called a
uniform distribution
.
Suppose that when we start the game, we start generating 'random' numbers
by walking through the table. Because the number table does not change, it means
that every time we play the game, the same sequence of random numbers would be
generated. In order to avoid this problem, we can indicate in the beginning that we
should start at a
different
position in the table. The position where we start in the
table is also called the
seed
of the random number generator. Often, we take a value
for the seed that is different every time we start the program, such as the current
system time.
So how do we use this random number generator to create randomness in our
game world? Suppose that we want to spawn an enemy 75 % of the times that a user
steps through a door. In that case, we generate a random number between 0 and 1.
If the number is smaller or equal to 0.75, we spawn an enemy, otherwise we do not.
Because of the uniform distribution this will lead exactly to the behavior that we
require. If we want to calculate a random speed between
1
2
and 1, we generate a
random number between 0 and 1, we divide this number by 2, and we add
1
2
. Let us
look how we can incorporate randomness in our
Painter
game.
In C#, generating a random number is done by using the
Random
class from
the
System
library. Therefore, in order to use this class, we need an extra
using
instruction:
using
System;
In order to use the random number generator, we need an instance of the
Random
class. It is likely that this instance needs to be accessed by different classes, and we
can also assume that a game only needs one random number generator. Therefore,
we again add a static member variable
random
to the
Painter
class, together with a
static property
Random
to access it. Have a look at the
Painter
class in the
Painter6
program to see the actual code.
After that, we can access the
random
object to generate random numbers for us.
We can use the method
NextDouble
from the
Random
class for that. This method
returns a random value of type
double
that is between 0
.
0 and 1
.
0.
double
someRandomNumber = Painter.Random.NextDouble();
The variable
someRandomNumber
now contains a value between 0
.
0 and 1
.
0.