-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
53 lines (41 loc) · 1.56 KB
/
Simulation.java
File metadata and controls
53 lines (41 loc) · 1.56 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
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
public class Simulation {
public static void main(String[] args) {
int numDice = 2;
Dice dice = new Dice(numDice);
Bins bin = new Bins(numDice, numDice*6);
for (int i = 0; i <1000000; i++) {
bin.incrementBin(dice.tossAndSum());
}
printResults(1000000, bin );
}
public static List<Double> tally(Integer numOfTosses, Bins bin) {
List<Double> percent = new ArrayList<Double>();
for (Integer element : bin.getBoxes()){
double tallyPercent = ((double)element / numOfTosses);
BigDecimal bigD = new BigDecimal(Double.toString(tallyPercent));
bigD = bigD.setScale(2, RoundingMode.HALF_UP);
percent.add(bigD.doubleValue()); //adds bigD into percent array
}
return percent;
}
public static void printResults(Integer numOfTosses, Bins bins){
List<Double> results = tally(numOfTosses, bins);
Integer diceNum = bins.getBinMin();
Integer numOfStars = 0;
String printResults = "";
for (Double element : results) {
numOfStars = (int)(element * 100);
printResults += String.format("%3d: %7d %1.2f ", diceNum, bins.getBin(diceNum), element);
for (int j = 1; j < numOfStars; j++) {
printResults += "*";
}
diceNum++;
printResults += "\n";
}
System.out.println(printResults);
}
}