5.3 Simulating the Game of Craps
Consider the game of “craps” as played in Las Vegas. The basic rules of the game are as follows: one player, the “shooter,” rolls a pair of dice. If the outcome of that roll is a 2, 3, or 12, the shooter immediately loses; if it is a 7 or an 11, the shooter wins. In all other cases, the number the shooter rolls on the first toss becomes the “point,” which the shooter must try to duplicate on subsequent rolls. If the shooter manages to roll the point before rolling a 7, the shooter wins; otherwise the shooter loses. It may take several rolls to determine whether the shooter wins or loses. After the first roll, only a 7 or the point have any significance until the win or loss is decided. Using the JSL random and statistic packages give answers and corresponding estimates to the following questions. Be sure to report your estimates in the form of confidence intervals.
- Before the first roll of the dice, what is the probability the shooter will ultimately win?
- What is the expected number of rolls required to decide the win or loss?
The solution to this problem involves the use of the Statistic class to collect the probability that the shooter will win and for the expected number of rolls. The following code illustrates the approach. The DUniformRV
class is used to represent the two dice. Two statistics are created to estimate the probability of winning and to estimate the expected number of rolls required to decide a win or a loss. A for loop is use to simulate 5000 games. For each game, the logic of winning, losing or matching the first point. When the game is ended the instances of Statistic
are used to collect the outcomes.
= new DUniformRV(1, 6);
DUniformRV d1 = new DUniformRV(1, 6);
DUniformRV d2 = new Statistic("Prob of winning");
Statistic probOfWinning = new Statistic("Number of Toss Statistics");
Statistic numTosses int numGames = 5000;
for (int k = 1; k <= numGames; k++) {
boolean winner = false;
int point = (int) d1.getValue() + (int) d2.getValue();
int numberoftoss = 1;
if (point == 7 || point == 11) {
// automatic winner
= true;
winner } else if (point == 2 || point == 3 || point == 12) {
// automatic loser
= false;
winner } else { // now must roll to get point
boolean continueRolling = true;
while (continueRolling == true) {
// increment number of tosses
++;
numberoftoss// make next roll
int nextRoll = (int) d1.getValue() + (int) d2.getValue();
if (nextRoll == point) {
// hit the point, stop rolling
= true;
winner = false;
continueRolling } else if (nextRoll == 7) {
// crapped out, stop rolling
= false;
winner = false;
continueRolling }
}
}
.collect(winner);
probOfWinning.collect(numberoftoss);
numTosses}
= new StatisticReporter();
StatisticReporter reporter .addStatistic(probOfWinning);
reporter.addStatistic(numTosses);
reporterSystem.out.println(reporter.getHalfWidthSummaryReport());
As we can see from the results of the statistics reporter, the probability of winning is just a little less than 50 percent.
Half-Width Statistical Summary Report - Confidence Level (95.000)%
Name Count Average Half-Width
--------------------------------------------------------------------------------
Prob of winning 5000 0.4960 0.0139
Number of Toss Statistics 5000 3.4342 0.0856
--------------------------------------------------------------------------------
Now let’s look at a slightly more complex static simulation.