|
@@ -1,5 +1,74 @@
|
1
|
1
|
public class Simulation {
|
2
|
2
|
|
|
3
|
+ public int numberOfRolls;
|
|
4
|
+ public int numberOfDice;
|
|
5
|
+ public int[] array;
|
|
6
|
+ public double[] probabilityArray;
|
|
7
|
+ public int[] numberOfStars;
|
|
8
|
+ public String[] printStars;
|
3
|
9
|
|
|
10
|
+ Bins bins = new Bins();
|
4
|
11
|
|
5
|
|
-}
|
|
12
|
+ public Simulation(int numberOfDice, int numberOfRolls) {
|
|
13
|
+ this.numberOfRolls = numberOfRolls;
|
|
14
|
+ this.numberOfDice = numberOfDice;
|
|
15
|
+ this.array = new int[(this.numberOfDice * 6) - numberOfDice + 1];
|
|
16
|
+ this.probabilityArray = new double[array.length];
|
|
17
|
+ this.numberOfStars = new int[array.length];
|
|
18
|
+ this.printStars = new String[array.length];
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ public void runSimulation(){
|
|
22
|
+ sim();
|
|
23
|
+ probability();
|
|
24
|
+ getNumberOfStars();
|
|
25
|
+ stringStars();
|
|
26
|
+ printResults();
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+// public int[] addToBinArray(int sumOfDice) {
|
|
30
|
+// array[sumOfDice - numberOfDice] = array[sumOfDice - numberOfDice] + 1;
|
|
31
|
+// return array;
|
|
32
|
+// }
|
|
33
|
+
|
|
34
|
+ public void sim(){
|
|
35
|
+ Dice dice = new Dice();
|
|
36
|
+ for(int i = 0; i < numberOfRolls; i++){
|
|
37
|
+ bins.addToBinArray(dice.tossAndSum(numberOfDice), array, numberOfDice);
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public void probability(){
|
|
42
|
+ for(int i = 0; i < array.length; i++){
|
|
43
|
+ probabilityArray[i] = (double)array[i] / numberOfRolls;
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public void getNumberOfStars(){
|
|
48
|
+ for(int i = 0; i < array.length; i++){
|
|
49
|
+ numberOfStars[i] = (int)(probabilityArray[i] * 100);
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public void stringStars(){
|
|
54
|
+ for(int j = 0; j < numberOfStars.length; j++) {
|
|
55
|
+ printStars[j] = "";
|
|
56
|
+ for (int i = 0; i < numberOfStars[j]; i++) {
|
|
57
|
+ printStars[j] = printStars[j] + "*";
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public void printResults() {
|
|
63
|
+ System.out.println("***");
|
|
64
|
+ System.out.println("Simulation of " + numberOfDice + " dice tossed for " + numberOfRolls + " times.");
|
|
65
|
+ System.out.println("***");
|
|
66
|
+ System.out.print("\n");
|
|
67
|
+ for (int i = 0; i < array.length; i++) {
|
|
68
|
+ //System.out.println(array[i]);
|
|
69
|
+ //System.out.println((i + 2) + " :" + array[i] + ": " + probabilityArray[i] + " " + printStars[i]);
|
|
70
|
+ System.out.printf("%2d :%9d: %.2f %s\n" ,(i + numberOfDice),array[i],probabilityArray[i],printStars[i]);
|
|
71
|
+ //System.out.printf(i + " " + numberOfStars);
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+}
|