CS 1073 Introductory Programming
|
public class Dice0 {
public static final int N = 1000000;
public static void main(String[] args) {
int d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0;
for (int k = 0; k < N; k++) {
int roll = (int)(6.0*Math.random() + 1.0);
if (roll == 1) d1++;
else if (roll == 2) d2++;
else if (roll == 3) d3++;
else if (roll == 4) d4++;
else if (roll == 5) d5++;
else if (roll == 6) d6++;
}
System.out.println("Rolls: " + N +
", 1: " + d1 +
", 2: " + d2 +
", 3: " + d3 +
", 4: " + d4 +
", 5: " + d5 +
", 6: " + d6);
}
}
Here are results of runs with N successive powers of 10:
Rolls: 100, 1: 19, 2: 13, 3: 21, 4: 14, 5: 20, 6: 13
Rolls: 1000, 1: 152, 2: 185, 3: 179, 4: 172, 5: 145, 6: 167
Rolls: 10000, 1: 1694, 2: 1667, 3: 1687, 4: 1645, 5: 1610, 6: 1697
Rolls: 100000, 1: 16846, 2: 16719, 3: 16619, 4: 16548, 5: 16632, 6: 16636
Rolls: 1000000, 1: 166586, 2: 166533, 3: 166734, 4: 166315, 5: 166832, 6: 167000
public class Dice1 {
public static final int N = 10000;
public static void main(String[] args) {
int[] d = new int[7];
for (int i = 1; i < 7; i++) d[i] = 0;
for (int k = 0; k < N; k++) {
int roll = (int)(6.0*Math.random() + 1.0);
d[roll]++;
}
System.out.print("Rolls: " + N);
for (int i = 1; i < 7; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
public class Dice2 {
public static final int N = 10000;
public static void main(String[] args) {
int[] d = new int[13];
for (int i = 2; i < 13; i++) d[i] = 0;
for (int k = 0; k < N; k++) {
int roll1 = (int)(6.0*Math.random() + 1.0);
int roll2 = (int)(6.0*Math.random() + 1.0);
int spots = roll1 + roll2;
d[spots]++;
}
System.out.print("Rolls: " + N);
for (int i = 2; i < 13; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
Here is the output:
Rolls: 10000, 2: 267, 3: 572, 4: 841, 5: 1117, 6: 1391, 7: 1599, 8: 1381, 9: 1178, 10: 801, 11: 571, 12: 282
Here is the same program, where the rolling of one die has been separated off into a separate function. Other parts have been simplified a bit. The output is the same, except that the output is random, depending on the values returned by the random number generator. (The code in red is new to this program.)
public class Dice3 {
public static final int N = 10000;
public static int roll() {
return (int)(6.0*Math.random() + 1.0);
}
public static void main(String[] args) {
int[] d = new int[13];
for (int i = 1; i < 13; i++) d[i] = 0;
for (int k = 0; k < N; k++) {
d[roll() + roll()]++;
}
System.out.print("Rolls: " + N);
for (int i = 2; i < 13; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
Finally, here is the program with an extra array holding the expected fraction of rolls of each number of spots that should occur. (The code in red is new to this program.)
public class Dice4 {
public static final int N = 3600000; // total rolls
public static int roll() {
return (int)(6.0*Math.random() + 1.0);
}
public static void main (String[] args) {
int[] s = new int[13]; // counters to record rolls
double[] t = {0., 0., // expected fraction of rolls
1./36., 2./36., 3./36., 4./36., 5./36., 6./36.,
5./36., 4./36., 3./36., 2./36., 1./36};
// roll N times, record results in array s
for (int i = 0; i < N; i++)
s[roll() + roll()]++;
// print results
System.out.println("Number of rolls: " + N + "\n");
System.out.println("Spots\tRolls\tExpected Rolls\n");
for (int i = 2; i < 13; i++) {
System.out.println(i + "\t" + s[i] + "\t" + t[i]*N);
}
}
}
And here is the output:
Number of rolls: 3600000 Spots Rolls Expected Rolls 2 100178 100000.0 3 200196 200000.0 4 299310 300000.0 5 399117 400000.0 6 501018 500000.0 7 601217 600000.0 8 499200 500000.0 9 400152 400000.0 10 299723 300000.0 11 199673 200000.0 12 100216 100000.0