4. What is the function of the int argument to the void function exit?
■ A RANDOM NUMBER GENERATOR
A random number generator is a function that returns a “randomly chosen” number. It
is unlike the functions we have seen so far in that the value returned is not determined
by the arguments (of which there are usually none) but rather by some global condi-
tions. Since you can think of the value returned as being a random number, you can
use a random number generator to simulate random events, such as the result of throw-
ing dice or flipping a coin. In addition to simulating games of chance, random number
generators can be used to simulate things that strictly speaking may not be random but
that appear to us to be random, such as the amount of time between the arrival of cars
at a toll booth.
100 Function Basics
The C++ library with header file
<cstdlib> contains a random number function
named
rand. This function has no arguments. When your program invokes
rand, the
randfunction returns an integer in the range
0 to
RAND_MAX, inclusive. (The number gener-
ated might be equal to
0 or
RAND_MAX.)
RAND_MAX is a defined integer constant whose
RAND_MAXdefinition is also in the library with header file
<cstdlib>. The exact value of
RAND_MAXis system-dependent but will always be at least
32767 (the maximum two-byte positive
integer). For example, the following outputs a list of ten “random” numbers in the
range
0 to
RAND_MAX:
int i;for (i = 0; i < 10; i++) cout << rand( ) << endl;You are more likely to want a random number in some smaller range, such as the
range 0 to 10. To ensure that the value is in the range 0 to 10 (including the end
points), you can use
rand( ) % 11This is called scaling. The following outputs ten “random” integers in the range 0 to
scaling10 (inclusive):
cout << (rand( ) % 11) << endl;Random number generators, such as the function
rand, do not generate truly ran-
dom numbers. (That’s the reason for all the quotes around “random.”) A sequence of
calls to the function
rand (or almost any random number generator) will produce a
sequence of numbers (the values returned by
rand) that appear to be random. How-
ever, if you could return the computer to the state it was in when the sequence of calls
to
rand began, you would get the same sequence of “random numbers.” Numbers that
pseudorandom numberappear to be random but really are not, such as a sequence of numbers generated by
calls to
rand, are called pseudorandom numbers.
A sequence of pseudorandom numbers is usually determined by one number known
as the seed. If you start the random number generator with the same seed, over and
seedover, then each time it will produce the same (random-looking) sequence of numbers.
You can use the function
srand to set the seed for the function
rand. The
void function
srandsrand takes one (positive) integer argument, which is the seed. For example, the follow-
ing will output two identical sequences of ten pseudorandom numbers:
srand(99);Predefined Functions 101
There is nothing special about the number
99, other than the fact that we used the
same number for both calls to
srand.
Note that the sequence of pseudorandom numbers produced for a given seed might
be system-dependent. If rerun on a different system with the same seed, the sequence
of pseudorandom numbers might be different on that system. However, as long as you
are on the same system using the same implementation of C++, the same seed will pro-
duce the same sequence of pseudorandom numbers.
P
SEUDORANDOM N
UMBERSThe function rand takes no arguments and returns a pseudorandom integer in the range 0 to RAND_MAX (inclusive). The void function srand takes one argument, which is the seed for the random number generator rand. The argument to srand is of type unsignedint, so the argu-ment must be nonnegative. The functions rand and srand, as well as the defined constant RAND_MAX, are defined in the library cstdlib, so programs that use them must contain the following directives:#include <cstdlib>using namespace std;These pseudorandom numbers are close enough to true random numbers for most
applications. In fact, they are often preferable to true random numbers. A pseudoran-
dom number generator has one big advantage over a true random number generator:
The sequence of numbers it produces is repeatable. If run twice with the same seed
value, it will produce the same sequence of numbers. This can be very handy for a
number of purposes. When an error is discovered and fixed, the program can be rerun
with the same sequence of pseudorandom numbers as those that exposed the error.
Similarly, a particularly interesting run of the program can be repeated, provided a
pseudorandom number generator is used. With a true random number generator every
run of the program is likely to be different.
Display 3.4 shows a program that uses the random number generator
rand to “pre-
dict” the weather. In this case the prediction is random, but some people think that is
about as good as weather prediction gets. (Weather prediction can actually be very
accurate, but this program is just a game to illustrate pseudorandom numbers.)
Note that in Display 3.4, the seed value used for the argument of
srand is the
month times the day. That way if the program is rerun and the same date is entered, the
same prediction will be made. (Of course, this program is still pretty simple. The pre-
diction for the day after the 14th may or may not be the same as the 15th, but this pro-
gram will do as a simple example.)
floating-pointProbabilities are usually expressed as a floating-point number in the range 0.0 to
random
Bạn đang xem 4. - ABSOLUTE C PHẦN 3 PPS