In order to create games and simluations, programmers use a random number generator to get a random number. A more appropriate term would be a pseudo random number gernerator, since the numbers actualy come from a very varied number sequence. What programmer's need to know is how to get these random numbers into a range they can use.
The first type of rand() function is one that returns any integer from zero to the largest possible random number. The first technique is to shrink the range so that it returns zero to a number. This is easily done with modulus division. Since it returns a remainer, and remainder are always less than the divisor, this will shrink the range to zero to the divisor-1.
rand() % 7; //returns a number between 0 and 6
The second technique is to get this between any range of integers. Let's say between negative three and positive five. Now let's think. If we add three to the ends of that range we get zero to eight. We already know how to get a number between zero and eight, so we just subtract three to make the random number between minus three and five. Notice how it is caculated.
int Hi = 8, Lo = -3;
rand() % (Hi-Lo) + (Lo); //returns a number between 0 and 6
The second type of a rand() function is between one and zero. But how do you get that from zero to the maximum random number? If the random number is divided by the maximum, then a random fraction is form between zero and one.
double randf()
{
return (double) rand() / max;
}
Now with a random decimal zero to one, how do we get one in a range? Like before if the low number is added then the range goes from zero to Hi+Lo. Now if that range is multipled by a random fraction, then a random percent i.e. number is returned.
int Hi = 5, Lo = -3;
randf() * (Hi-Lo) + Lo;
Lowest | Highest | Formula |
---|---|---|
0 | RAND_MAX | rand() |
0 | Hi | rand() % Hi+1 |
Lo | Hi | rand() % (Hi - Lo) + Lo |
0 | 1 | rand() / RAND_MAX |
Lo (decimal) | Hi (decimal) | (rand()/RAND_MAX) * (Hi - Lo) + Lo |
As stated before random number gerenators are not random at all. It often is a math function with results that seem random. The number that it starts with is called a seed. Therefore if a program uses the same seed, it will return the same "random" results everytime it is run. (This is what happens in the tornado pattern of Airman's attacks from the Nintendo video game Megaman 2.) This is useful for error checking in the program's developement but not useful in applications. (Unless you like exploited a game's glitch.)
To compensate for this random number gernerators may provide a Randomize() function. This ensures that the seed is random everytime the program is run. How is this acoomplished, in case a Randomize is not included? Just make the seed the value of the current time. Since no program can be run at the very same time over and over again, the seed will always be different. It may take a little reasearch, but most languages have a function to return the computer system time.
seed(3); //always the same pattern every time it runs.
randomize(); //randomizes the numbers
seed((int)time()); //this is how randomize() does it
Prev -- Back to Portal -- Next