simulate a pair of dice.

Simulation.java 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import java.util.TreeMap;
  2. public class Simulation
  3. {
  4. private int numberOfDice;
  5. private int numberOfRolls;
  6. public Simulation(int numberOfDice, int numberOfRolls){
  7. this.numberOfDice = numberOfDice;
  8. this.numberOfRolls = numberOfRolls;
  9. }
  10. public void runSimulation(){
  11. Simulation sims = new Simulation(2, 1000000);
  12. Dice dice = new Dice(numberOfDice);
  13. Bins results = new Bins();
  14. int currentRoll = 0;
  15. while(currentRoll < numberOfRolls){
  16. int result = dice.roll();
  17. currentRoll++;
  18. results.increment(result);
  19. }
  20. printResults(results);
  21. }
  22. public void printResults(Bins bin){
  23. System.out.println("*******************************\n" +
  24. "Rolling 2 Dice 1,000,000 times \n" +
  25. "*******************************");
  26. for (int i = 2; i <= 12; i++){
  27. Float percent = (bin.getN(i) /1000000f);
  28. System.out.println(String.format(i + ": " + bin.getN(i) + "%5.2f" + " : ", percent));
  29. }
  30. }
  31. }