Computer Science 2611
Fall 2000
Laboratory Assignment 6

Handed out: 9/28/00

Pseudo-random number generators

Sometimes it is useful for a computer to produce a random number. Most computers do not have the ability to produce a genuinely random number, but there are methods for creating numbers that are apparently random.

The idea is to produce a function called next. You start with an integer called the seed. Define r0 to be the seed. Then define

r1 = next(r0)
r2 = next(r1)
r3 = next(r2)
etc. For example, suppose that function next is defined so that next(n) = (17*n + 1) mod 23. Choose the seed to be 2. Then the sequence of numbers is
r0 = 2
r1 = next (2) = 12
r2 = next (12) = 21
r3 = next (21) = 13
So the sequence of "apparently" random numbers starts 12, 21, 13, ... (We won't use the seed as one of the pseudo-random numbers.) What is the next number in the sequence? Notice that the numbers in this sequence will never be larger than 22. Also, the sequence repeats after a while. It goes 12, 21, 13, 15, 3, 6, 11, 4, 0, 1, 18, 8, 22, 7, 5, 17, 14, 9, 16, 20, 19, 2, 12, 21, 13, 15, ...

Different pseudo-random number generators use different functions next . One kind of functions uses next(n) = (a*n + b) mod c, where a and c are prime numbers, a < c and b < c. Generally, you choose c to be fairly large, so that it takes a long time before the sequence starts to repeat.


Pseudo-random number generating objects

Suppose that the class of random number generator objects is called Random. Class Random has one constructor and one public method.

Constructor
When a random number generator object is created, it must have a seed value installed. Random R(n) creates random number generator R with seed set to n.

nextRandom
R.nextRandom() returns the next random number in the sequence for random number generator R. It must update the current value that R is holding, so that several calls to R.nextRandom() produce different numbers. It just replaces the current value of R by the next value in the sequence, and returns that new value.

For example, here is a function that produces the sum of n pseudo-random numbers. It is only given as an example of using a pseudo-random number generator; this function will not be part of your assignment. It sets the initial seed to 35.

   int randomSum(int n)
   {
     int k, sum;
     Random R(35);
     k = 0;
     sum = 0;
     while(k < n) {
       sum = sum + R.nextRandom();
       k++;
     }
     return sum;
   }

Here is another example function. It performs a "coin toss", producing either true or false. It does not want to create a new random number generator for each coin toss, so it has a random number generator as a parameter. Notice that, since the random number generator is an object, it is passed by reference.

   bool cointoss(Random& R)
   {
     return R.nextRandom() % 2;
   }

The assignment, part 1

Write a class of objects that are pseudo-random number generators. (This is the class Random of the example above.) Use a next function next(n) = (a*n + b) mod c, choosing numbers a, b and c to be fairly large prime numbers. Keep them less than 32,000, or there will be problems with arithmetic overflow. Be sure that a and c are different prime numbers, or your pseudo-random number generator will produce poor results.

Write a main program to test the pseudo-random number generator. Just have it print the first 20 numbers generated, after setting the seed. Do the numbers look reasonable? Do not move on to part 2 until you have a working pseudo-random number generator class.

Hints

Draw a picture of a pseudo-random number generator object. What variable(s) does it need? What public methods does it have? Does it have any private methods?

Note that the constants a, b and c are not variables, they are constants. They are only used in one place, the next function. So build them into the next function, not into the object. You can declare a constant using the keyword const. For example, to declare constant b (of type long) to be 917, use

    const long b = 917;

To test whether a number is prime, you can just run a program. For your benefit, here is a function that will tell whether a number is prime. It will help you to choose reasonable values for a, b and c.

   bool isPrime(long n)
   {
     if(n < 2) return 0;

     long k = 2;
     while(k*k <= n) {
       if(n % k == 0) return 0;
       k++;
     }
     return 1;
   }

Assignment part 2: Using the random number generator

Write a program that plays Craps. It should read an integer n from the user and play n games of Craps. At the end of the games, it should report how many of the games were passed, and how many did not pass, and the fraction of the games that passed.

The rules of Craps are as follows. The game is played with two dice. The player throws the dice to begin. If the outcome (the sum of the dice) is 7 or 11, the player passes. If the outcome is 2, 3 or 12, the player does not pass.

If the outcome of the first throw is anything except 2,3,7,11 or 12, then the outcome of the first throw becomes the players point. The player repeatedly tosses the dice until either he or she throws a value equal to the point (in which case the player passes) or the player throws a 7 (in which case the player does not pass).

Here is a sample output.

    How many games shall I play?  4
    Game 1. (7) Pass
    Game 2. (3) No pass
    Game 3. (4,8,5,7) No pass
    Game 4. (8,10,8) Pass

    There were 2 passes and 2 nonpasses, for a fraction of 0.500 passes.
Notice that the rolls of the dice are shown.

Break your program into simple functions. For example, you will want a function that throws a die (just take a random number mod 6, and add 1). Encapsulate the rules of Craps in a function, so that no other function needs to know anything about those rules.


What to turn in

Turn in a printout of your solution to the second part. Be sure that it is well-commented and well-indented.