Beginning C++ Game Programming
上QQ阅读APP看书,第一时间看更新

Random numbers

Random numbers are useful for lots of reasons in games. Perhaps you could use them for determining what card the player is dealt, or how much damage within a certain range is subtracted from an enemy's health. As hinted at, we will use random numbers to determine the starting location and the 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 take a look at the syntax and the steps required with some hypothetical code.

Computers can't actually 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. Take a look at this code, which seeds the random number generator:

// Seed the random number generator with the time 
srand((int)time(0)); 

The code above gets the time from the PC using the time function like this 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 returned from time to an int. This is required by the srand function in this situation.

Note

A conversion from one type to another is called a cast.

So, in summary, this is what happens in the previous line of code:

  • It gets the time using time
  • It converts it to type int
  • It 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 a random number between a range, and save it to a variable for later use:

// 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 really useful for generating a random speed and starting location for our bees and clouds.

We will do this soon, but we first need to learn how to make decisions in C++.