|
@@ -1,3 +1,4 @@
|
|
1
|
+import javax.sound.midi.Soundbank;
|
1
|
2
|
import java.util.Formatter;
|
2
|
3
|
import java.util.HashMap;
|
3
|
4
|
import java.util.Map;
|
|
@@ -8,20 +9,13 @@ public class Simulation {
|
8
|
9
|
private Dice dice;
|
9
|
10
|
private Bins bins;
|
10
|
11
|
private int numberOfDie;
|
|
12
|
+ protected Simulation simulation;
|
11
|
13
|
|
12
|
14
|
public Simulation(int numberOfDie, int numberOfThrows) {
|
13
|
15
|
this.numberOfThrows = numberOfThrows;
|
14
|
16
|
this.dice = new Dice(2);
|
15
|
17
|
this.bins = new Bins(numberOfDie, numberOfDie * 6);
|
16
|
18
|
}
|
17
|
|
-
|
18
|
|
-//
|
19
|
|
-// public void simulationToString() {
|
20
|
|
-// StringBuilder sb = new StringBuilder();
|
21
|
|
-// for (Map.Entry entry : bins.incrementBin) {
|
22
|
|
-// System.out.println(entry + "\n");
|
23
|
|
-// }
|
24
|
|
-// }
|
25
|
19
|
public void runSimulation() {
|
26
|
20
|
for (int i = 0; i < numberOfThrows; i++) {
|
27
|
21
|
int value = dice.diceRoll();
|
|
@@ -30,10 +24,34 @@ public class Simulation {
|
30
|
24
|
}
|
31
|
25
|
}
|
32
|
26
|
|
|
27
|
+ public String simulationToString() {
|
|
28
|
+ StringBuilder sb = new StringBuilder();
|
|
29
|
+ for (Integer keys:bins.recordOfRolls.keySet()) {
|
|
30
|
+ sb.append(keys +": "+ bins.recordOfRolls.get(keys) + "\n");
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ return sb.toString();
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public String getPercentage(){
|
|
37
|
+ StringBuilder sb = new StringBuilder();
|
|
38
|
+ Double sum = 0.0;
|
|
39
|
+ for (Integer i : bins.recordOfRolls.values()){
|
|
40
|
+ sum += i;
|
|
41
|
+ }
|
|
42
|
+ for (Integer keys : bins.recordOfRolls.values()) {
|
|
43
|
+ Double percent = keys / sum;
|
|
44
|
+ sb.append(percent);
|
|
45
|
+ }
|
|
46
|
+ return sb.toString();
|
|
47
|
+ }
|
|
48
|
+
|
33
|
49
|
public static void main(String[] args) {
|
34
|
|
- Simulation sim = new Simulation(2, 10);
|
|
50
|
+ Simulation sim = new Simulation(2, 10000);
|
35
|
51
|
sim.runSimulation();
|
36
|
|
-// sim.simulationToString();
|
|
52
|
+ System.out.println(sim.simulationToString());
|
|
53
|
+ System.out.println(sim.getPercentage());
|
|
54
|
+
|
37
|
55
|
}
|
38
|
56
|
|
39
|
57
|
|