-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
54 lines (45 loc) · 1.42 KB
/
Simulation.java
File metadata and controls
54 lines (45 loc) · 1.42 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
51
52
53
54
import java.util.ArrayList;
import java.util.List;
public class Simulation {
int numOfDice;
int numOfRolls;
Dice dice;
Bins bins;
public Simulation(int numOfDice, int numOfRolls) {
this.numOfDice = numOfDice;
this.numOfRolls = numOfRolls;
}
public void runSimulation(){
int result;
this.bins = new Bins(numOfDice, numOfDice * 6);
this.dice = new Dice(numOfDice);
for(int i = 0; i < numOfRolls; i++) {
result = dice.tossAndSum();
bins.incrementBin(result);
}
System.out.println("");
}
public void printResults() {
for(int i = numOfDice; i <= numOfDice * 6; i++) {
Double percentage = (double) bins.getBin(i) / numOfRolls;
System.out.printf(
"%2d : %7d: %.2f ", i, bins.getBin(i), percentage);
for(int j = 1; j < percentage * 100; j++) {
System.out.print("*");
}
System.out.println();
}
}
public double getPercentage(int numOfRolls, Bins bins) {
double percentage = 0;
for(int element : bins.getBins()) {
percentage = ((double) element / numOfRolls);
}
return percentage;
}
public static void main(String[] args) {
Simulation simulation = new Simulation(2, 1000000);
simulation.runSimulation();
simulation.printResults();
}
}