Game Development Reference
In-Depth Information
// Create a Random object to generate random
// numbers. The object is seeded with the time
// in milliseconds since Jan 1, 1970.
Random random = new Random();
double x;
// Retrieve 8 floating-point random numbers
// between 0 and 1.
for(int j=0; j<8; ++j) {
x = random.nextDouble();
System.out.println("x = "+x);
}
}
}
Here is a typical output when the RandomDemo code is executed. Because the Random object
will have a different seed each time the program is run, the output from the code will be different
each time as well.
x = 0.12166301656769563
x = 0.7040696650721638
x = 0.6330423734744145
x = 0.7314827972956403
x = 0.299262216506065
x = 0.8277476660869888
x = 0.02470816980699342
x = 0.9898129649660115
To see that the random numbers generated by the Random class aren't truly random, change
the line that creates the Random object so that the seed is set to a fixed value.
Random random = new Random(1234);
When this modification is made, the Random object will always use the same starting point,
and the same sequence of “random” numbers is returned every time the code is run.
Probability Functions
A probability function , also referred to as a probability distribution function , is simply a
mathematical expression or geometrical curve that models the probability of an event occur-
ring within certain dimensions. Probability functions are used in scientific modeling all the
time. The energy state of an individual air molecule in a volume of air is characterized using a
probability function. There are many cases in game programming where probability functions
could be used as well. For example, a probability function might be used to model the speed of
the opponent snowmobiles in a snowmobile race game.
Usually a probability function is defined by a mathematical equation. In some situations
there is an equal likelihood of all possible outcomes. For example, when a die is rolled there is
Search WWH ::




Custom Search