| 12345678910111213141516171819202122232425262728293031323334353637 |
- import java.util.TreeMap;
- public class Simulation
- {
- private int numberOfDice;
- private int numberOfRolls;
-
- public Simulation(int numberOfDice, int numberOfRolls){
- this.numberOfDice = numberOfDice;
- this.numberOfRolls = numberOfRolls;
- }
-
- public void runSimulation(){
- Simulation sims = new Simulation(2, 1000000);
- Dice dice = new Dice(numberOfDice);
- Bins results = new Bins();
-
- int currentRoll = 0;
- while(currentRoll < numberOfRolls){
- int result = dice.roll();
- currentRoll++;
- results.increment(result);
- }
- printResults(results);
- }
-
- public void printResults(Bins bin){
- System.out.println("*******************************\n" +
- "Rolling 2 Dice 1,000,000 times \n" +
- "*******************************");
- for (int i = 2; i <= 12; i++){
- Float percent = (bin.getN(i) /1000000f);
- System.out.println(String.format(i + ": " + bin.getN(i) + "%5.2f" + " : ", percent));
- }
- }
-
- }
|