# Chapter 19 – Random Numbers

Random numbers are used in many types of programs.  An example is the Windows solitaire game.  Each time you deal the cards, the deck is arranged differently.  Let's say you would like a program to print 10 random numbers, each between 0 and 9.  Can a computer truly generate random numbers for you?  The answer is no, but for practical purposes, the answer is yes.  They can simulate random numbers by using a complicated mathematical algorithm.  A simplified way to view this would be to look at the front cover of this book which contains pi printed out to thousands of digits.  Pi is an infinitely long series of numbers which contains no repeating patterns.  Your computer program could take the next digit of pi every time it prints a random number between 0 and 9.  If your program prints 10 of these random numbers, then it would print: 3141592653.

C++ has a random number generated located in the stdlib.h library.  The rand( ) function returns a random positive integer .  If you were mod this integer by 50, then the result (remainder of the division) would be a random number between 0 and 49:

cout << rand() % 50;      //Outputs a random number between 0 and 49

cout << rand() % 50 + 1;
//Outputs a random number between 1 and 50

The program below prints two random numbers between 0 and 49, inclusively.

 Program 19a – Random Number Generator #include using namespace std; int main() {    cout << rand() % 50 << endl;    cout << rand() % 50 << endl;    return 0; } Output – Note: Your output may be different numbers 33 36

If you try running the preceding program multiple times, you will notice that it prints the same two numbers every time.  This is because the random number algorithm starts at the beginning every time.  Each time you call the rand() function in a program, it will give you a random number.  But when you run the program again, it will output the same numbers?  Why?  Because programs do the same thing every time they run.

To correct this problem, random number algorithms need to be seeded with some external data that will be different each time it runs.  Seeding allows an external variable to be introduced into the random number algorithm so that it will produce different results.  The most popular way of seeding the random number generator is to use time.

The time.h library allows you to access the computer’s clock.  The time is used to seed the random number generator.  This seeding (srand function) only needs to be executed once at the beginning of a program.  Each time you run the program, the time has changed.  Therefore, the random number generator can produce different results.  The next program is similar to previous, but with the addition of seeding the random number generator.

 Program 19b – Seeding the Random Number Generator #include #include       //Library containing time function using namespace std; int main() {    srand(time(NULL));  //Seed the random number generator with time    cout << rand() % 50 << endl;    cout << rand() % 50 << endl;    return 0; } Output – Note: Your output may be different numbers 3 43

Try running the preceding program multiple times so you can verify that it prints different numbers each time.  Now that you understand how computer random number generators work, here's a question:  Two Windows computers are set to the exact same time.  The solitaire program is opened on each computer at the exact same time.  Will they show the same cards?

The next program is an example of how to print a random statement to the screen.

 Program 19c – Printing A Random Statement #include #include       using namespace std; int main() {    srand(time(NULL));    int R = rand() % 3 + 1;     //R is a random number between 1 and 3    cout << "I like to eat ";    if (R == 1)       cout << "snails\n";    if (R == 2)       cout << "worms\n";    if (R == 3)       cout << "pizza\n";    return 0; } Output – Note: Your output may be different I like to eat pizza