-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
48 lines (38 loc) · 1.38 KB
/
Simulation.java
File metadata and controls
48 lines (38 loc) · 1.38 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
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 bins = new Bins(numberOfDice, maxValue);
for (int i = 0; i < numberOfTosses ; i++) {
bins.incrementBin(dice.tossAndAdd());
}
return bins;
}
private void printResults(Bins bins){
Integer maxValue = numberOfDice * 6;
for (int i = numberOfDice; i < maxValue ; i++) {
System.out.println(String.format("%2d : %4d : %1.2f ",i, bins.getBin(i),binPercent(bins.getBin(i))));
printHash(binPercent(bins.getBin(i)));
System.out.println();
}
}
public void printHash(Double Value){
int numberOfHash = (int)(Value * 100);
for (int i = 1; i <= numberOfHash ; i++) {
System.out.print(" # ");
}
}
public Double binPercent(Integer Value){
return Double.valueOf(Value) /numberOfTosses;
}
}