diff --git a/XiongResults.md b/XiongResults.md new file mode 100644 index 0000000..66cecef --- /dev/null +++ b/XiongResults.md @@ -0,0 +1,19 @@ +#Xiong's Results + +```java +*** +Simulation of 2 dice tossed for 1000000 times. +*** + + 2 : 27758: 0.03 ** + 3 : 55522: 0.06 ***** + 4 : 83600: 0.08 ******** + 5 : 110834: 0.11 *********** + 6 : 138949: 0.14 ************* + 7 : 166409: 0.17 **************** + 8 : 138581: 0.14 ************* + 9 : 111219: 0.11 *********** +10 : 83506: 0.08 ******** +11 : 55580: 0.06 ***** +12 : 28042: 0.03 ** +``` diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..f524f42 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,53 @@ public class Bins { + int two = 0; + int three = 0; + int four = 0; + int five = 0; + int six = 0; + int seven = 0; + int eight = 0; + int nine = 0; + int ten = 0; + int eleven = 0; + int twelve = 0; + + public void storeBin(int result) { + switch (result) { + case 2: + two++; + break; + case 3: + three++; + break; + case 4: + four++; + break; + case 5: + five++; + break; + case 6: + six++; + break; + case 7: + seven++; + break; + case 8: + eight++; + break; + case 9: + nine++; + break; + case 10: + ten++; + break; + case 11: + eleven++; + break; + default: + twelve++; + break; + } + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..075b4fa 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,27 @@ public class Dice { + public int faceValue; + + public Dice() { + this.faceValue = (int)((Math.random() * 6) + 1); + } + + public static Integer diceToss2() { + Dice dice1 = new Dice(); + Dice dice2 = new Dice(); + + return dice1.faceValue + dice2.faceValue; + } + +// public static Integer diceToss5() { +// Dice dice1 = new Dice(); +// Dice dice2 = new Dice(); +// Dice dice3 = new Dice(); +// Dice dice4 = new Dice(); +// Dice dice5 = new Dice(); +// +// return dice1.faceValue + dice2.faceValue + dice3.faceValue + dice4.faceValue + dice5.faceValue; +// } + } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..3d6015d 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,35 @@ public class Simulation { + public static void main(String[] args) { + Simulation simulate = new Simulation(); + simulate.simulate2Dice(1000000); + + + } + + public void simulate2Dice(int timesToSimulate) { + int counter = 0; + Bins results = new Bins(); + while (counter < timesToSimulate) { + results.storeBin(Dice.diceToss2()); + counter++; + } + System.out.println("***"); + System.out.println(String.format("Simulation of 2 dice tossed for %d times.", timesToSimulate)); + System.out.println("***\n"); + System.out.println(String.format(" 2 : %8d: %5.2f **", results.two, (double)results.two/timesToSimulate)); + System.out.println(String.format(" 3 : %8d: %5.2f *****", results.three, (double)results.three/timesToSimulate)); + System.out.println(String.format(" 4 : %8d: %5.2f ********", results.four, (double)results.four/timesToSimulate)); + System.out.println(String.format(" 5 : %8d: %5.2f ***********", results.five, (double)results.five/timesToSimulate)); + System.out.println(String.format(" 6 : %8d: %5.2f *************", results.six, (double)results.six/timesToSimulate)); + System.out.println(String.format(" 7 : %8d: %5.2f ****************", results.seven, (double)results.seven/timesToSimulate)); + System.out.println(String.format(" 8 : %8d: %5.2f *************", results.eight, (double)results.eight/timesToSimulate)); + System.out.println(String.format(" 9 : %8d: %5.2f ***********", results.nine, (double)results.nine/timesToSimulate)); + System.out.println(String.format("10 : %8d: %5.2f ********", results.ten, (double)results.ten/timesToSimulate)); + System.out.println(String.format("11 : %8d: %5.2f *****", results.eleven, (double)results.eleven/timesToSimulate)); + System.out.println(String.format("12 : %8d: %5.2f **", results.twelve, (double)results.twelve/timesToSimulate)); + + } }