# Selections in C++: Generating Random Numbers

You can use the rand() function to obtain a random integer.

Suppose you want to develop a program for a first-grader to practice subtraction. The pro­gram randomly generates two single-digit integers, numberl and number2, with numberl >= number2, and it displays to the student a question such as “What is 9 – 2?” After the student enters the answer, the program displays a message indicating whether it is correct.

To generate a random number, use the rand() function in the cstdlib header file. This function returns a random integer between 0 and RAND_MAX. RAND_MAX is platform-dependent constant. In Visual C++, RAND_MAX is 32767.

The numbers rand() produces are pseudorandom. That is, every time it is executed on the same system, rand() produces the same sequence of numbers. On the author’s machine, for example, executing these three statements will always produce the numbers 130, 10982, and 1090.

cout << rand() << endl << rand() << endl << rand() << endl;

Why? The rand() function’s algorithm uses a value called the seed to control how to gen­erate the numbers. By default, the seed value is 1. If you change the seed to a different value, the sequence of random numbers will be different. To change the seed, use the srand(seed) function in the cstdlib header file. To ensure that the seed value is different each time you run the program, use time(0). As discussed in Section 2.10, “Case Study: Displaying the Current Time,” invoking time(0) returns the current time in seconds elapsed since the time 00:00:00 on January 1, 1970 GMT. So, the following code will display a random integer with a random seed.

srand(time(0));

cout << rand() << endl;

To obtain a random integer between 0 and 9, use

rand() % 10

The program may be set up to work as follows:

Step 1: Generate two single-digit integers into number1 and number2.

Step 2: If number1 < number2, swap number1 with number2.

Step 3: Prompt the student to answer “What is number1 – number2?”

Step 4: Check the student’s answer and display whether it is correct.

The complete program is shown in Listing 3.4.

Listing 3.4 SubtractionQuiz.cpp

1 #include <iostream>

2 #include <ctime> // for time function

3 #include <cstdlib> // for rand and srand functions

4 using namespace std;

5

6 int main()

7 {

8    // 1. Generate two random single-digit integers

9     srand(time(0));

10    int number1 = rand() % 10;

11    int number2 = rand() % 10;

12

13    // 2. If number1 < number2, swap number1 with number2

14    if (number1 < number2)

15    {

16       int temp = number1;

17       number1 = number2;

18       number2 = temp;

19     }

20

21     // 3. Prompt the student to answer “what is number1 – number2?”

22     cout << “What is ” << number1 << ” – ” << number2 << “? “;

25

27     if (number1 – number2 == answer)

28     cout << “You are correct!”;

29     else

30     cout << “Your answer is wrong. ” << number1 << ” – ” << number2

31     << ” should be ” << (number1 – number2) << endl;

32

33      return 0;

34 }

To swap two variables numberl and number2, a temporary variable temp (line 16) is first used to hold the value in numberl. The value in number2 is assigned to numberl (line 17) and the value in temp is assigned to number2 (line 18).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.