-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
48 lines (40 loc) · 1.53 KB
/
Simulation.java
File metadata and controls
48 lines (40 loc) · 1.53 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
import java.util.*;
public class Simulation {
private int numberOfDies;
private int numberOfTosses;
public Simulation(Integer numberOfDies, Integer numberOfTosses) {
this.numberOfDies = numberOfDies;
this.numberOfTosses = numberOfTosses;
}
public Integer[] runSimulation() {
Dice die = new Dice(numberOfDies);
Bins results = new Bins(numberOfDies, numberOfDies * 6);
for (int i = 0; i < numberOfTosses; i++) {
int diceRoll = die.tossAndSum();
results.incrementBin(diceRoll);
}
return results.diceRolls;
}
public void printResults() {
Integer[] diceRolls = runSimulation();
StringBuilder builder = new StringBuilder();
System.out.printf("***\nSimulation of %s dice tossed for %s times.\n***\n\n", numberOfDies, numberOfTosses);
for (int i = 0; i < diceRolls.length; i++) {
for (int j = 0; j < (diceRolls[i] / numberOfTosses) * 100; i++) {
builder.append("*");
}
System.out.printf("%s : %s: %.2f %s\n", i + numberOfDies, diceRolls[i], (double)diceRolls[i] / numberOfTosses, builder.toString());
builder.delete(0, builder.length());
}
}
public Integer getNumberOfDies() {
return numberOfDies;
}
public Integer getNumberOfTosses() {
return numberOfTosses;
}
public static void main(String[] args) {
Simulation simulation = new Simulation(2, 1000000);
simulation.printResults();
}
}