-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
50 lines (39 loc) · 1.51 KB
/
Simulation.java
File metadata and controls
50 lines (39 loc) · 1.51 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 {
private Integer numberOfDice;
private Integer numberOfTosses;
public Simulation(Integer numberOfDice, Integer numberOfTosses) {
this.numberOfDice = numberOfDice;
this.numberOfTosses = numberOfTosses;
}
public static void main(String[] args) {
Simulation simulation = new Simulation(2, 10000);
simulation.printResults(simulation.runSimulation());
}
private Bins runSimulation() {
Dice dice = new Dice(numberOfDice);
Integer maxValue = numberOfDice * 6;
Bins bin = new Bins(numberOfDice, maxValue);
for (int i = 0; i < numberOfTosses; i++) {
bin.incrementBin(dice.tossAndSum());//increments to random numbers between start and end
}
return bin;
}
private void printResults(Bins bin) {
Integer maxValue = numberOfDice * 6;
//loop through results array and divide by the total number of tosses
for (int i = numberOfDice; i <= maxValue ; i++) {
System.out.print(String.format("%2d : %4d : %1.2f ", i, bin.getBin(i), binPercent(bin.getBin(i))));
printStars(binPercent(bin.getBin(i)));
System.out.println();
}
}
public void printStars(Double value) {
int numberOfStars = (int) (value * 100);
for (int i = 1; i <= numberOfStars; i++) {
System.out.print("*");
}
}
public double binPercent(Integer value) {
return Double.valueOf(value) / numberOfTosses;
}
}