|
@@ -1,21 +1,29 @@
|
|
1
|
+import java.util.Arrays;
|
|
2
|
+
|
1
|
3
|
public class Simulation {
|
2
|
|
-private int numberOfDies;
|
3
|
|
-private int numberOfTosses;
|
|
4
|
+ private int numberOfDies;
|
|
5
|
+ private int numberOfTosses;
|
4
|
6
|
|
5
|
|
-public Simulation(){
|
6
|
|
-}
|
|
7
|
+ public static void main(String[] args) {
|
|
8
|
+ Simulation simulation = new Simulation(2, 1000000);
|
|
9
|
+ Bins bins = simulation.runSimulation();
|
|
10
|
+ simulation.printResult(bins);
|
|
11
|
+ }
|
7
|
12
|
|
8
|
|
-public Simulation (int numberOfDies, int numberOfTosses){
|
9
|
|
- this.numberOfDies = numberOfDies;
|
10
|
|
- this.numberOfTosses = numberOfTosses;
|
11
|
|
-}
|
|
13
|
+ public Simulation(){
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public Simulation (int numberOfDies, int numberOfTosses){
|
|
17
|
+ this.numberOfDies = numberOfDies;
|
|
18
|
+ this.numberOfTosses = numberOfTosses;
|
|
19
|
+ }
|
12
|
20
|
|
13
|
21
|
|
14
|
|
- public Bins getArrayOfSum(int numberOfToss) {
|
|
22
|
+ public Bins runSimulation() {
|
15
|
23
|
Dice dice = new Dice(this.numberOfDies);
|
16
|
24
|
Bins bins = new Bins(this.numberOfDies, this.numberOfDies * 6);
|
17
|
25
|
|
18
|
|
- for (int i = 0; i < numberOfToss; i++) {
|
|
26
|
+ for (int i = 0; i < this.numberOfTosses; i++) {
|
19
|
27
|
int sum = dice.tossAndSum();
|
20
|
28
|
bins.incrementBin(sum);
|
21
|
29
|
}
|
|
@@ -23,18 +31,16 @@ public Simulation (int numberOfDies, int numberOfTosses){
|
23
|
31
|
return bins;
|
24
|
32
|
}
|
25
|
33
|
|
26
|
|
- public String buildStats(Bins bins){
|
27
|
|
- for (int i = numberOfDies; i <= numberOfDies * 6; i++){
|
|
34
|
+ public void printResult(Bins bins){
|
|
35
|
+ for (int i = numberOfDies; i <= (numberOfDies * 6); i++){
|
28
|
36
|
int number = i;
|
29
|
37
|
int count = bins.getBin(number);
|
30
|
|
-
|
31
|
|
- System.out.println(bins.getBin(i));
|
|
38
|
+ double fraction = ((double) count)/ numberOfTosses;
|
|
39
|
+ System.out.print(String.format("%3d :%9d: %3.2f%% ", number, count, fraction));
|
|
40
|
+ for (int j = 0; j <= (fraction * 100); j++) {
|
|
41
|
+ System.out.print("*");
|
|
42
|
+ }
|
|
43
|
+ System.out.println();
|
32
|
44
|
}
|
33
|
|
-
|
34
|
|
- }
|
35
|
|
-
|
36
|
|
- public static void main(String[] args) {
|
37
|
|
- Simulation simulation = new Simulation(2, 1000000);
|
38
|
|
-
|
39
|
45
|
}
|
40
|
46
|
}
|