Question
In the 17th century, the discipline of probability theory got its start when a gambler asked a mathematician friend to explain some observations about dice
In the 17th century, the discipline of probability theory got its start when a gambler asked a mathematician friend to explain some observations about dice games. Why did he, on average, lose a bet that at least one six would appear when rolling a die four times? And why did he seem to win a similar bet, getting at least one double six when rolling a pair of dice 24 times?
Nowadays, it seems astounding that any person would roll a pair of dice 24 times in a row, and then repeat that many times over. Lets do that experiment on a computer instead. Simulate each game a million times and print out the wins and losses.
Use the files in the zip file for Assignment 1 as a starting point.
There is a Die class which simulates the throw of a die.
There is a Die Odds Tester class which you should use as a test program to show your
program working.
The GameSimulator class is where you write your code. Please fill in all the code where
it is indicated.
DieOddsTester:
/** This program simulates the wins and losses for two different games of dice. */ public class DiceOddsTester { public static void main(String[] args) {
GameSimulator simulator = new GameSimulator(6, 1000000);
simulator.runSingleDieSimulation(); System.out.println("Game #1 wins: " + simulator.getWinPercent()); System.out.println("Expected: .51");
simulator.runDoubleDieSimulation(); System.out.println("Game #2 wins: " + simulator.getWinPercent()); System.out.println("Expected: .49"); } }
Die:
import java.util.Random; /** This class models a die that, when cast, lands on a random face. */ public class Die { private Random generator; private int sides;
/** Constructs a die with a given number of sides. @param s the number of sides, e.g., 6 for a normal die. */ public Die(int s) { sides = s; generator = new Random(); }
/** Simulates a throw of the die. @return the face of the die. */ public int cast() { return 1 + generator.nextInt(sides); } }
GameSimular:
/** This program sumulates the wins and losses for two different games of dice. */ public class GameSimulator { private int tries; private Die d1, d2; private int wins; private int losses;
/** Construct a simulator object for die games. @param numSides the number of sides on the die. @param numTries the number of times to run the simulation. */ public GameSimulator(int numSides, int numTries) { . . . }
/** Runs a single die simulation. One die is cast 4 times. If a six appears in those 4 casts, then wins is incremented, otherwise losses is incremented. Simulation is run according to the number of tries set. */ public void runSingleDieSimulation() { . . . }
/** Runs a double die simulation. A pair of dice are cast 24 times. If a double-six appears in those 24 casts, then wins is incremented, otherwise losses is incremented. The simulation is run according to the number of tries set. */ public void runDoubleDieSimulation() { . . . }
/** Returns the % of wins. @return the % of wins. */ public double getWinPercent() { return (double)(wins) / (wins + losses); }
/** Returns the number of wins. @return the number of wins. */ public int getWins() { return wins; }
/** Returns the number of losses. @return the number of losses. */ public int getLosses() { return losses; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started