diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..0b1b44e Binary files /dev/null and b/.DS_Store differ diff --git a/java/.DS_Store b/java/.DS_Store new file mode 100644 index 0000000..0654db2 Binary files /dev/null and b/java/.DS_Store differ diff --git a/java/src/.DS_Store b/java/src/.DS_Store new file mode 100644 index 0000000..e8c3d61 Binary files /dev/null and b/java/src/.DS_Store differ diff --git a/java/src/MichaelSieResults.md b/java/src/MichaelSieResults.md new file mode 100644 index 0000000..e69de29 diff --git a/java/src/main/.DS_Store b/java/src/main/.DS_Store new file mode 100644 index 0000000..c05dfaf Binary files /dev/null and b/java/src/main/.DS_Store differ diff --git a/java/src/main/java/Bins.class b/java/src/main/java/Bins.class new file mode 100644 index 0000000..1180a4b Binary files /dev/null and b/java/src/main/java/Bins.class differ diff --git a/java/src/main/java/Bins.java b/java/src/main/java/Bins.java index b9da83e..e53d8ca 100644 --- a/java/src/main/java/Bins.java +++ b/java/src/main/java/Bins.java @@ -1,4 +1,25 @@ public class Bins { + int s; + int[] x; + int minimum; + int maximum; + + public Bins(int min, int max) { + minimum = min; + maximum = max; + s = max - min + 1; + x = new int[s]; + } + + public void incrementBin(int value){ + int i = value - minimum; + x[i]++; + } + + public int getBin(int value){ + int i = value - minimum; + return x[i]; + } } diff --git a/java/src/main/java/Dice.class b/java/src/main/java/Dice.class new file mode 100644 index 0000000..2fd968c Binary files /dev/null and b/java/src/main/java/Dice.class differ diff --git a/java/src/main/java/Dice.java b/java/src/main/java/Dice.java index 2283c96..fc5ce48 100644 --- a/java/src/main/java/Dice.java +++ b/java/src/main/java/Dice.java @@ -1,4 +1,23 @@ + +import java.util.Random; + + public class Dice { + int d; + + public Dice(int n){ + d = n; + } + public int tossAndSum() { + Random rand = new Random(); + int r; + int sum = 0; + for(int i= 0; i < d; i++){ + r = rand.nextInt(6-1+1)+1; + sum = sum + r; + } + return sum; + } -} +} \ No newline at end of file diff --git a/java/src/main/java/Simulation.class b/java/src/main/java/Simulation.class new file mode 100644 index 0000000..d2d7e33 Binary files /dev/null and b/java/src/main/java/Simulation.class differ diff --git a/java/src/main/java/Simulation.java b/java/src/main/java/Simulation.java index 73d86e8..cefe445 100644 --- a/java/src/main/java/Simulation.java +++ b/java/src/main/java/Simulation.java @@ -1,5 +1,73 @@ +import java.util.Scanner; + public class Simulation { + int nDice; + int nToss; + int min; + int max; + Dice dice; + Bins bins; + + + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int nDice = getInput(scanner, "Please enter number of dice: "); + int nToss = getInput(scanner, "Please enter number of toss: "); + + Simulation sim = new Simulation(nDice, nToss); + sim.runSimulation(); + sim.printResult(); + scanner.close(); + + } + + public Simulation(int nDice, int nToss){ + this.nDice = nDice; + this.nToss = nToss; + min = nDice; + max = nDice * 6; + dice = new Dice(nDice); + bins = new Bins(min, max); + } + public void runSimulation(){ + for(int i = 1; i <= nToss; i++){ + int result = dice.tossAndSum(); + bins.incrementBin(result); + } + } + public void printResult(){ + for(int i = min; i <= max; i++){ + int count = bins.getBin(i); + double prob = (double) count / nToss; + int starsCount = (int) (prob * 100); + System.out.printf("%2d : %7d : %.2f ", i, count, prob); + for(int j = 0; j < starsCount; j++){ + System.out.print("*"); + } + System.out.println(); + } + } + + public static int getInput(Scanner scanner, String Prompt){ + while(true){ + System.out.print(Prompt); + if(!scanner.hasNextInt()){ + System.out.println("Invalid Input. Please input a number."); + scanner.next(); + continue; + } + int value = scanner.nextInt(); + + if(value <= 0){ + System.out.println("Please enter positive number."); + continue; + } + return value; + } } +} + diff --git a/java/target/classes/Bins.class b/java/target/classes/Bins.class new file mode 100644 index 0000000..0c61107 Binary files /dev/null and b/java/target/classes/Bins.class differ diff --git a/java/target/classes/Dice.class b/java/target/classes/Dice.class new file mode 100644 index 0000000..8096f66 Binary files /dev/null and b/java/target/classes/Dice.class differ diff --git a/java/target/classes/Simulation.class b/java/target/classes/Simulation.class new file mode 100644 index 0000000..7e8644e Binary files /dev/null and b/java/target/classes/Simulation.class differ diff --git a/java/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/java/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..6e78974 --- /dev/null +++ b/java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,3 @@ +/Users/michael/Projects/DiceyLab/java/src/main/java/Simulation.java +/Users/michael/Projects/DiceyLab/java/src/main/java/Bins.java +/Users/michael/Projects/DiceyLab/java/src/main/java/Dice.java