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)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
r2 = next(r1)
r3 = next(r2)
r0 = 2So 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, ...
r1 = next (2) = 12
r2 = next (12) = 21
r3 = next (21) = 13
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.
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; }
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.
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; }
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.