CS 1220
Object Oriented Analysis and Design
Spring Semester 2005
ASSIGNMENT #2
DUE 2/1/2005
Write a program to simulate
the game of craps. The rules are as
follows:
Roll a pair of dice. If the sum of the spots is 7 or 11, you win
immediately. If the sum of the spots is
two (snake-eyes), three (craps) or 12 (boxcars), you lose immediately. Otherwise, the number you rolled is called
your “point”. You keep rolling until
you get either (1) your point, in which case you win, or (2) a seven, in which
case you lose.
Run the program 3 times
simulating 100, 1000 and 10000 games of craps and calculate the following
statistics for each case:
1. The probability of winning on the first roll
2. The probability of losing on the first roll
3. The probability of winning a game (first or subsequent
rolls)
4. The probability of losing a game (first or subsequent
rolls)
Print all probabilities to
the nearest thousandth (3 places to the right of the decimal). Print the actual mathematical probabilities
(given below), the calculated or simulated probabilities and the percentage
difference between them. In general,
the more games included in the simulation, the closer the simulated
probabilities will be to the actual probabilities which means the percentage
differences will be less. However, this
pattern will not be true in every case.
If the percentage difference is too small, it may show as all zeros
since you are printing only 3 decimal places.
The percentage difference
should be calculated by taking the absolute value of the difference of the
Actual and Simulated probabilities divided by the Actual and multiplied by
100.0.
Percent Difference = ( abs ( Actual - Simulated) / Actual
) * 100.0
Use the random number
functions named srand ( ) and rand ( ) which are available in
C++. At the beginning of your program,
you must input a seed value which is used to initialize the random number
generator. The seed value should be an
odd number of your choice, usually at least 5 digits long:
srand
( Seed ) ; // Seed should be an odd integer value
To generate a dice value of
one through six, use the rand (
) function which generates a positive random integer in the valid integer range
for the local machine. To map the value
onto a one to six range, use the modulus operator to divide by 6 (which returns
a remainder of zero to five) and add one:
Dice1 =
rand ( ) % 6 + 1 ;
Calculate Dice2 in the same
manner and the sum yields your total for a roll of a pair of dice.
The report format should be:
Craps Simulation
Number of games in simulation: xxxxx
Actual Simulated %
Difference
Win on first roll .223 .xxx xxx.xxx
Lose on first roll .112 .xxx xxx.xxx
Total Win .497 .xxx xxx.xxx
Total Lose .503 .xxx xxx.xxx
All numeric values should be
right justified with the decimal point aligned.