Sometimes we need to have a program produce a number randomly, or in other words "pick a number - any number". Such a number is called a "random number". The action of producing it is called "random number generation".
Most computer programming languages have a function for this action. The function is typically named "rand" or "random" or something like that. Some of these functions return integers; others return floating point numbers. All of them return a number selected from within a specific range of values. In C++, the function name is rand and it returns an integer (typically from a range of values no less than 0 through 32,767 inclusive). The rand function is defined within a C++ header library named "cstdlib" and requires the use of the compiler directive
#include <cstdlib>
in the declaration section at the top of the code. Then, to produce a random number, you use the C++ function call
rand()
at the point in your code where you want to the number produced. Thus, to store a random number in a variable named N, you would write the C++ statement
N = rand();
Alternatively, to generate and display a random number on the screen, you would write the C++ statement
cout << rand();
Because a computer is incapable of creative thought, it cannot actually generate a true random number. Instead, it must start with a number given to it by the programmer called a "seed" and perform a collection of complex operations on that value to produce a number that appears to be random. We call these almost random numbers "pseudo-random numbers". Any program that uses a set of these pseudo-random numbers will always use the same set of them every time the program runs unless a different seed is provided by the programmer when the program starts. If a programmer wants a program to produce a different set of random numbers each time the program is run, he or she must provide a new seed using the C++ function call
srand(X);
at the start of the program, where X represents a new seed value. But this is a problem, because the programmer is not present every time a program is run. The solution is to use a function that is always guaranteed to produce a different number in place of X. One such function is the time function which produces a number representing the number of seconds elapsed since midnight January 1, 1970, according to the system clock in your computer. Because this value is based on the passage of time, it will always be different. So you can use the time function in place of X in the srand function. The time function requires a parameter in parentheses to indicate where to store the value returned by the function. Since we prefer to use this value rather than store it, we will provide the special parameter of 0 for the time function and produce a unique seed for the srand function with the C++ statement
srand(time(0));
at the beginning of the program. The time function is defined within a C++ header library named "ctime" and requires the use of the compiler directive
#include <ctime>
in the declaration section at the top of the code.
Based on the explanation of the time function given above, it would seem that we could produce pseudo-random numbers without the need for either the rand or srand functions by simply using the time function. This is true, but the result would be a very large random number representing the number of seconds elapsed since midnight January 1, 1970. If we only wanted a number between 1 and 10, we would need to figure out some way to use a pseudo-random number returned by the time or rand function to produce a new number within the restricted range. This can be done using the following generic formula
N = rand() % R + L;
in which N is the desired random number, R is the range of values from which that number is selected, and L is the lowest allowed value. For example, if you wanted N to receive a number between 1 and 10 inclusive, the value of L would be 1 and R would be 10, and the C++ statement would be
N = rand() % 10 + 1;
Alternatively, if you wanted N to receive a number between 4 and 10 inclusive, the value of L would be 4 and R would be 7 (the quantity of values that are possible from 4 to 10), and the C++ statement would be
N = rand() % 7 + 4;