|
@@ -1,5 +1,75 @@
|
|
1
|
+import java.text.DecimalFormat;
|
|
2
|
+import java.util.*;
|
|
3
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
4
|
+
|
1
|
5
|
public class Simulation {
|
2
|
6
|
|
|
7
|
+ public Simulation (int min, int max, int tosses) {
|
|
8
|
+
|
|
9
|
+ int binCounter = 0;
|
|
10
|
+ int tossCounter = min;
|
|
11
|
+ int mapCounter = min;
|
|
12
|
+
|
|
13
|
+ HashMap<Integer, Integer> map = new HashMap<>();
|
|
14
|
+
|
|
15
|
+ // building HashMap for results
|
|
16
|
+
|
|
17
|
+ while (mapCounter <= max) {
|
|
18
|
+ map.put(mapCounter, 0);
|
|
19
|
+ mapCounter++;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ // Running Simulation
|
|
23
|
+
|
|
24
|
+ for (int i = 1; i <= tosses; i++) {
|
|
25
|
+ int result = ThreadLocalRandom.current().nextInt(min, max);
|
|
26
|
+ int increment = map.get(result);
|
|
27
|
+ map.put(result, increment + 1);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ /* while (tossCounter < tosses) {
|
|
31
|
+
|
|
32
|
+ int result = ThreadLocalRandom.current().nextInt(min, max);
|
|
33
|
+ map.put(tossCounter, result);
|
|
34
|
+
|
|
35
|
+ tossCounter++; */
|
|
36
|
+
|
|
37
|
+ Bins localstorage = new Bins(map);
|
|
38
|
+
|
|
39
|
+ //printing results
|
|
40
|
+ System.out.print(map + System.getProperty("line.separator"));
|
|
41
|
+
|
|
42
|
+ int counter = min;
|
|
43
|
+ String output = "";
|
|
44
|
+
|
|
45
|
+ for (int y = min; y <= max; y++) {
|
|
46
|
+
|
|
47
|
+ // calculate percentage
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+ int getTosses = map.get(y);
|
|
51
|
+ double percentage = (double)getTosses/max;
|
|
52
|
+ DecimalFormat df = new DecimalFormat("#0.00");
|
|
53
|
+ String percent = df.format(percentage);
|
|
54
|
+ double convert = Double.parseDouble(percent);
|
|
55
|
+
|
|
56
|
+ // calculate stars
|
|
57
|
+
|
|
58
|
+ double z = 0.01;
|
|
59
|
+ String stars = "";
|
|
60
|
+
|
|
61
|
+ while (z < convert) {
|
|
62
|
+ stars += "*";
|
|
63
|
+ z = z + 0.01;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ // print each line
|
|
67
|
+
|
|
68
|
+ output += String.format("%3d", y) + " :" + String.format("%9d", getTosses) + ": " + String.format("%.2f", convert) + " " + stars + System.getProperty("line.separator");
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ System.out.println(output);
|
3
|
72
|
|
|
73
|
+ }
|
4
|
74
|
|
5
|
|
-}
|
|
75
|
+ }
|