diff --git a/pom.xml b/pom.xml index 7219542..31de3c4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + junit + junit + 4.13.2 + test + + \ No newline at end of file diff --git a/src/main/MannyResults.md b/src/main/MannyResults.md new file mode 100644 index 0000000..e693ca1 --- /dev/null +++ b/src/main/MannyResults.md @@ -0,0 +1,14 @@ +~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~ +Simulation of 2 dice tossed for 1,000,000 times +~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~ +2 : 27575 : 0.03 ** +3 : 55480 : 0.06 ***** +4 : 83778 : 0.08 ******** +5 : 110976 : 0.11 *********** +6 : 138851 : 0.14 ************* +7 : 167160 : 0.17 **************** +8 : 138546 : 0.14 ************* +9 : 111153 : 0.11 *********** +10 : 83432 : 0.08 ******** +11 : 55314 : 0.06 ***** +12 : 27735 : 0.03 ** diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..fc5f031 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,32 @@ +import java.util.Arrays; public class Bins { + private int min; + private int max; + Integer[] sumOfRolls ; + + public Bins(int min, int max) { + this.min = min; + this.max = max; + this.sumOfRolls = new Integer[(max - min) + 1]; + + Arrays.fill(sumOfRolls, 0); + } +// for (int i = 0; i < sumOfRolls.length; i++) { +// sumOfRolls[i] = 0; +// } + + + public Integer getBin(int sum) { // getting the sum of rolls for the specific number you're tracking + int indexOfSum = sum - min; + return sumOfRolls[indexOfSum]; + } + + public void incrementBin(int binIndex){ + int index = binIndex - min; + sumOfRolls[index]++; + } + + } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..5aaabba 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,19 @@ public class Dice { + private final int numberOfDice; + + public Dice(int diceNum) { + this.numberOfDice = diceNum; + } + + public int tossAndSum(){ + int sum = 0; + for (int i = 0; i < numberOfDice; i++) { + sum += (int) ((Math.random() * 6) + 1); + } + return sum; + } } + + diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..4071e6d 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,51 @@ +import java.util.Arrays; + public class Simulation { + public static void main(String[] args) { + Simulation sim = new Simulation(2, 1000000); + sim.runSimulation(); + sim.printResults(); + } + + int numberOfDice; + int numberOfRolls; + Integer[] results; + + public Simulation(int numberOfDice, int numberOfRolls) { + this.numberOfDice = numberOfDice; + this.numberOfRolls = numberOfRolls; + } + + public Integer[] runSimulation() { + + Dice testDice = new Dice(numberOfDice); + Bins testBin = new Bins(numberOfDice, numberOfDice * 6); + + for (int i = 0; i < numberOfRolls; i++) { + int toss = testDice.tossAndSum(); + testBin.incrementBin(toss); + } + return testBin.sumOfRolls; + } + + private void printResults() { + Integer[] results = runSimulation(); + System.out.println("~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~\n" + + "Simulation of 2 dice tossed for 1,000,000 times\n" + + "~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~"); + for(int i = 0; i < results.length; i++) { + int index = i + 2; + int frequency = results[i]; + float percent = (float) results[i] / this.numberOfRolls; + int starValue = (int) (percent * 100); + System.out.printf("%2d : %8d : %.2f ", index, frequency, percent); + for (int j = 0; j < starValue; j++) { + System.out.print("*"); + } + System.out.println(); + } + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..e322015 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,19 @@ +import org.junit.Assert; +import org.junit.Test; + +public class BinsTest { + @Test + public void testBins() { + // Given + int testMin = 2; + int testMax = 12; + int expected = testMax - testMin + 1; + + // When + Bins testBin = new Bins(testMin, testMax); + int actual = testBin.sumOfRolls.length; + + // Then + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..4f4df72 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,19 @@ +import org.junit.Assert; +import org.junit.Test; + +public class DiceTest { + + @Test + public void testDice() { + // Given + int numberOfDice = 2; + Dice testDice = new Dice(numberOfDice); + + // When + Integer test = testDice.tossAndSum(); + + // Then + Assert.assertTrue(test < 13 && test > 1); + } + +}