Random numbers
Random numbers are useful for lots of reasons in games—perhaps determining what card the player is dealt or how much damage within a certain range is subtracted from an enemy's health. We will now learn how to generate random numbers to determine the starting location and speed of the bee and the clouds.
Generating random numbers in C++
To generate random numbers, we will need to use some more C++ functions—two more, to be precise. Don't add any code to the game yet. Let's just look at the syntax and the steps that are required with some hypothetical code.
Computers can't genuinely pick random numbers. They can only use algorithms/calculations to pick a number that appears to be random. So that this algorithm doesn't constantly return the same value, we must seed the random number generator. The seed can be any integer number, although it must be a different seed each time you require a unique random number. Look at the following code, which seeds the random number generator:
// Seed the random number generator with the time
srand((int)time(0));
The preceding code gets the time from the PC using the time function, that is, time(0). The call to the time function is enclosed as the value to be sent to the srand function. The result of this is that the current time is used as the seed.
The previous code is made to look a little more complicated because of the slightly unusual looking (int) syntax. What this does is convert/cast the value that's returned from time into an int. This is required by the srand function in this situation.
Important note
The term that's used to describe a conversion from one type to another is cast.
So, in summary, the previous line of code does the following:
- Gets the time using time
- Converts it into an int
- Sends this resulting value to srand, which seeds the random number generator
The time is, of course, always changing. This makes the time function a great way to seed the random number generator. However, think about what might happen if we seed the random number generator more than once and in such quick succession that time returns the same value. We will see and solve this problem when we animate our clouds.
At this stage, we can create the random number, between a range, and save it to a variable for later use, like so:
// Get the random number & save it to a variable called number
int number = (rand() % 100);
Notice the odd-looking way we assign a value to number. By using the Modulo operator (%) and the value of 100, we are asking for the remainder, after dividing the number returned from rand, by 100. When you divide by 100, the highest number you can possibly have as a remainder is 99. The lowest number possible is 0. Therefore, the previous code will generate a number between 0 and 99 inclusive. This knowledge will be useful for generating a random speed and starting location for our bees and clouds.
But before we can implement our random bees and clouds, we will need to learn how to make decisions in C++.