-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
50 lines (42 loc) · 1.37 KB
/
Simulation.java
File metadata and controls
50 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Simulation {
Integer numberOfDice;
Integer numberOfTosses;
Dice dice;
Bins bins;
public Simulation(Integer numberOfDice, Integer numberOfTosses) {
this.numberOfDice = numberOfDice;
this.numberOfTosses = numberOfTosses;
}
public void runSimulation() {
this.dice = new Dice(2);
this.bins = new Bins(2, 12);
for (int i = 0; i < numberOfTosses; i++) {
Integer sumUp = dice.tossAndSum();
bins.incrementBins(sumUp);
}
System.out.println("");
}
public void printResults() {
for(Integer i = 2; i <= 12; i++) {
Double percentage = (double) bins.getBinValue(i) / numberOfTosses;
System.out.printf("%2d : %7d: %.2f ", i, bins.getBinValue(i), percentage);
for(int j = 1; j < percentage * 100; j++)
{
System.out.print("*");
}
System.out.println();
}
}
public double getPercentage(Integer numberOfTosses, Bins bins) {
double percentage = 0;
for( int element : bins.getBins()) {
percentage = element/ numberOfTosses;
}
return percentage;
}
public static void main(String[] args) {
Simulation simulation = new Simulation(2, 1000000);
simulation.runSimulation();
simulation.printResults();
}
}