|
@@ -1,4 +1,77 @@
|
|
1
|
+import java.util.Random;
|
|
2
|
+
|
1
|
3
|
public class Dice {
|
2
|
4
|
|
|
5
|
+ // public static void main(String[] args) {
|
|
6
|
+
|
|
7
|
+// Random rand = new Random();
|
|
8
|
+// int freq[] = new int[13];
|
|
9
|
+//
|
|
10
|
+// for(int roll = 2; roll < 10000000; roll++){
|
|
11
|
+// ++freq[1+rand.nextInt(12)];
|
|
12
|
+// }
|
|
13
|
+//
|
|
14
|
+// System.out.println("Face\tFrquency");
|
|
15
|
+//
|
|
16
|
+// for(int face=2; face < freq.length;face++){
|
|
17
|
+// System.out.println(face+"\t"+"\t"+freq[face]);
|
|
18
|
+// }
|
|
19
|
+
|
|
20
|
+ public Random random;
|
|
21
|
+ private int sides;
|
|
22
|
+ private int amountOfDie;
|
|
23
|
+
|
|
24
|
+// public void dice(int numOfSides){
|
|
25
|
+// this.sides = numOfSides;
|
|
26
|
+// }
|
|
27
|
+
|
|
28
|
+ public Dice(int sides, int amountOfDie) {
|
|
29
|
+ this.sides = sides;
|
|
30
|
+ random = new Random();
|
|
31
|
+ this.amountOfDie = amountOfDie;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ // Rolling dice a million times
|
|
35
|
+// Dice dice1 = new Dice(6, 3);
|
|
36
|
+// Dice dice2 = new Dice(6, 2);
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+ public void rollDice(int numberOfRolls) {
|
|
43
|
+
|
|
44
|
+ int totals[] = new int[sides*sides]; //frequencies of the sums
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+ //initialize totals to zero
|
|
48
|
+ for (int i = 0; i < totals.length; i++) {
|
|
49
|
+ totals[i] = 0;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ //roll the dice
|
|
53
|
+ for (int roll = 1; roll <= numberOfRolls; roll++) {
|
|
54
|
+ int sum = 0;
|
|
55
|
+ for(int dice = 0; dice < amountOfDie; dice++){
|
|
56
|
+ int valueOfRoll = (int)(Math.floor(Math.random() * sides) + 1);
|
|
57
|
+ sum += valueOfRoll;
|
|
58
|
+ }
|
|
59
|
+ totals[roll] += sum;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ //print out the table
|
|
63
|
+ System.out.println( "Sum\tFrequency\tPercentage");
|
|
64
|
+
|
|
65
|
+ //ignore subscripts 0 & 1
|
|
66
|
+ for (int j = 2; j < totals.length; j++) {
|
|
67
|
+ double percent = (((double) totals[j] / (1000000)));
|
|
68
|
+ int reformPercent = (int) (Math.ceil(percent * 100));
|
|
69
|
+ System.out.printf("%3d%12d%12d\n", j, totals[j], reformPercent);
|
|
70
|
+ }//end for
|
|
71
|
+ }//end method
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+ }//end class
|
|
75
|
+
|
|
76
|
+
|
3
|
77
|
|
4
|
|
-}
|