[Note: The random-number generation techniques shown in this section and Section 6.7 are included for readers who are not yet using C++11 compilers. In Section 6.8, we’ll present C++11’s improved random-number capabilities.]
We now take a brief and hopefully entertaining diversion into a popular programming application, namely simulation and game playing. In this and the next section, we develop a game-playing program that includes multiple functions.
The element of chance can be introduced into computer applications by using the C++ Standard Library function rand. Consider the following statement:
i = rand();
The function rand
generates an unsigned integer between 0 and RAND_MAX
(a symbolic constant defined in the <cstdlib>
header). You can determine the value of RAND_MAX
for your system simply by displaying the constant. If rand
truly produces integers at random, every number between 0 and RAND_MAX
has an equal chance (or probability) of being chosen each time rand
is called.
The range of values produced directly by the function rand
often is different than what a specific application requires. For example, a program that simulates coin tossing might require only 0 for “heads” and 1 for “tails.” A program that simulates rolling a six-sided die would require random integers in the range 1 to 6. A program that randomly predicts the next type of spaceship (out of four possibilities) that will fly across the horizon in a video game might require random integers in the range 1 through 4.