-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
51 lines (38 loc) · 1.38 KB
/
Simulation.java
File metadata and controls
51 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
49
50
51
import java.util.Arrays;
public class Simulation {
public static void main(String[] args) {
Simulation sim = new Simulation(2, 1000000);
sim.runSimulation();
sim.printResults();
}
int numberOfDie;
int numberOfRolls;
Integer[] results;
public Simulation(int numberOfDie, int rolls){
this.numberOfDie = numberOfDie;
this.numberOfRolls = rolls;
}
public Integer[] runSimulation(){
Dice simDice = new Dice(this.numberOfDie);
Bins testBin = new Bins(this.numberOfDie,(this.numberOfDie*6));
for (int i = 0; i < this.numberOfRolls; i++) {
int simSum = simDice.tossAndSum();
testBin.incrementBin(simSum);
}
return testBin.sumOfRolls;
}
public void printResults() {
Integer[] results = runSimulation();
for (int i = 0; i < results.length; i++) {
int index = i+2;
int freq = results[i];
float percent = (float) results[i] / this.numberOfRolls;
int asterickValue = (int) (percent * 100); //casting takes precedence over order of operations
System.out.printf("%2d : %8d : %.2f ", index, freq, percent);
for (int j = 0; j < asterickValue; j++) {
System.out.print("*");
}
System.out.println();
}
}
}