CSC 306 Introduction to Programming with C++
Pseudorandom Numbers in C++
Chapter 6
Objectives
- Learn more details on using loops
- Introduce the C++ for loop
- Learn to create and use pseudo-random numbers in C++
- Learn new terminology:
Important Definitions
- for loop
- pseudo-random numbers
- random number seed
For Loops
Like the while-loop and the do-while loop, the for loop is another way
to repeat a sequence of instructions in C++.
Whereas a while-loop is typically used when the programmer does not know how many
times the loop will repeat (such as when waiting for a user to type in a certain
letter), the programmer usually knows how many iterations occur for for-loops.
It is important to realize that for-loops and while loops are structurally the
same.
They both have the three important components of loops:
- Initialization - the counter variable (if one is used) is assigned an
initial value.
- Condition of repeat - A conditional statement (recall they evaluate to either
true or false) is used to determine if the loop should repeat or stop.
- Counter variable update - note that without this step, the result is an
infinite loop.
Consider a program that is designed to print out the entire ASCII table of
characters.
Either a while or for loop can accomplish this goal as shown in the following code
fragments:
| WHILE LOOP | FOR LOOP
|
|
|
int number;
char character;
number = 32;
while (number <= 126) {
character = number;
cout << "The character '" << character;
cout << "' is represented as the number ";
cout << number << " in the computer.\n";
number++;
}
|
|
int number;
char character;
for (number = 32 ; number <= 126 ; number++) {
character = number;
cout << "The character '" << character;
cout << "' is represented as the number ";
cout << number << " in the computer.\n";
}
|
The general syntax for the for loop is:
|
|
for( initialization; repetition_condition ; update ) {
Statement 1;
...
Statement N;
}
|
Note that the update on the counter variable (the statement "number++" in the
example above) occurs in the third part within the parentheses of the for loop
and as the last statement in the body of the while loop.
Pseudo-random Numbers
A computer can only follow a specific set of predefined and well structured
instructions, so it is not possible to make a it generate a set of truly
random numbers.
They can, however, generate a set of numbers that appear to be random, but are
in fact repeatable.
These numbers are called pseudo-random, as they are falsely random, but
they satisfy all statistical tests for randomness.
A sequence of pseudo-random numbers on a computer begins with a random number
seed that starts the sequence.
If the seed is the same each time the program is run, the sequence of numbers
returned will be the same.
For example, suppose that a pseudo-random number generator exists called
genRand() that returns double numbers between 0 and one, and it is
used in the following function:
|
|
// This function will extract 6 pseudorandom numbers using the genRand()
// function.
void extractRandomness() {
double thisRand;
int numberExtracted=0;
while( numberExtracted < 6 ) {
thisRand = genRand();
numberExtracted++;
}
}
|
Running the program once will generate a sequence of numbers, such as
"0.45, 0.398, 0.956, 0.562, 0.874, 0.012".
Running the program again will generate the same sequence.
Having the same sequence generated each time is useful for debugging purposes but
not for some applications that rely on truly random numbers, such as games.
To generate a different random sequence each time, it is necessary to set a
different seed.
(Of course, now the seed must be different because otherwise the program starts
generating random numbers from the same point)
The standard way to set a different seed each time is to use the current clock
time because this value changes each time the program runs.
The RollDice.cpp program contains two
classes it uses to simulate rolloing dice: (1) a RandGen class that
generates pseudo-random numbers and (2) a Dice class.
Try running the program several times to see what it does.
Assignment Specifics
This assignment must be completed individually.
You are to use the Dice and RandGen classes given in the
RollDice.cpp program defined above to explore pseudo-random numbers on the
computer.
-
Write program that asks the user how many dice rolls they would like to
have and how many sides their dice will have.
-
Use a
for loop in your main program to roll the specified die
the given number of times, outputing to the console the result of each of
the rolls.
-
At the end of the loop, print out the average dice roll, the high dice roll,
and the low dice roll.
-
Add an error testing function to the dice class so that when a user specifies
the number of sides to be less than equal to 0, the dice is set to a six-sided
die by default and the user is given a warning.
Be sure to do the following:
When you are finished writing and testing your assignment, drop your source
code file into the CSC306_A12 dropbox on the Academic server.