-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
51 lines (41 loc) · 1.52 KB
/
Simulation.java
File metadata and controls
51 lines (41 loc) · 1.52 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 numberOfDice;
int numberOfRolls;
Integer[] results;
public Simulation(int numberOfDice, int numberOfRolls) {
this.numberOfDice = numberOfDice;
this.numberOfRolls = numberOfRolls;
}
public Integer[] runSimulation() {
Dice testDice = new Dice(numberOfDice);
Bins testBin = new Bins(numberOfDice, numberOfDice * 6);
for (int i = 0; i < numberOfRolls; i++) {
int toss = testDice.tossAndSum();
testBin.incrementBin(toss);
}
return testBin.sumOfRolls;
}
private void printResults() {
Integer[] results = runSimulation();
System.out.println("~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~\n"
+ "Simulation of 2 dice tossed for 1,000,000 times\n"
+ "~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~");
for(int i = 0; i < results.length; i++) {
int index = i + 2;
int frequency = results[i];
float percent = (float) results[i] / this.numberOfRolls;
int starValue = (int) (percent * 100);
System.out.printf("%2d : %8d : %.2f ", index, frequency, percent);
for (int j = 0; j < starValue; j++) {
System.out.print("*");
}
System.out.println();
}
}
}