CS 1713 -- Introduction to Computer Science
Dice Statistics
// Die.java: One die (the singular of "dice" is "die")
import java.util.*;
public class Die {
private Random r = new Random();
public int spots() {
return r.nextInt(6) + 1;
}
public int rollTwo() {
return spots() + spots();
}
}
// DiceStatistics.java: Collect and print results of rolling dice
public class DiceStatistics {
private int n;
private int s[] = new int[13];
private double t[] = {0.,0.,1./36.,2./36.,3./36.,4./36.,5./36.,
6./36.,5./36.,4./36.,3./36.,2./36.,1./36};
public DiceStatistics(int numRolls) {
n = numRolls;
}
private Die die = new Die();
public void runSim() {
for (int i = 0; i < n; i++)
s[die.rollTwo()]++;
}
public void printResults() {
System.out.println("Number of rolls: " + n + "\n");
System.out.println("Spots\tRolls\tFractions");
for (int i = 2; i < 13; i++) {
System.out.print(i + "\t" + s[i] + "\t");
System.out.print(s[i]/(double)n + " \t");
System.out.println(t[i]);
}
}
}
// DiceTest.java: test DiceStatistics class
public class DiceTest {
public static void main (String[] args) {
DiceStatistics dice = new DiceStatistics(1000000000);
dice.runSim();
dice.printResults();
}
}
Number of rolls: 100
Spots Rolls Fractions
2 2 0.02 0.027777777777777776
3 7 0.07 0.05555555555555555
4 11 0.11 0.08333333333333333
5 7 0.07 0.1111111111111111
6 10 0.1 0.1388888888888889
7 20 0.2 0.16666666666666666
8 11 0.11 0.1388888888888889
9 12 0.12 0.1111111111111111
10 9 0.09 0.08333333333333333
11 6 0.06 0.05555555555555555
12 5 0.05 0.027777777777777776
Number of rolls: 1000
Spots Rolls Fractions
2 18 0.018 0.027777777777777776
3 52 0.052 0.05555555555555555
4 67 0.067 0.08333333333333333
5 126 0.126 0.1111111111111111
6 133 0.133 0.1388888888888889
7 175 0.175 0.16666666666666666
8 146 0.146 0.1388888888888889
9 100 0.1 0.1111111111111111
10 103 0.103 0.08333333333333333
11 55 0.055 0.05555555555555555
12 25 0.025 0.027777777777777776
Number of rolls: 10000
Spots Rolls Fractions
2 274 0.0274 0.027777777777777776
3 526 0.0526 0.05555555555555555
4 837 0.0837 0.08333333333333333
5 1091 0.1091 0.1111111111111111
6 1382 0.1382 0.1388888888888889
7 1689 0.1689 0.16666666666666666
8 1387 0.1387 0.1388888888888889
9 1162 0.1162 0.1111111111111111
10 835 0.0835 0.08333333333333333
11 533 0.0533 0.05555555555555555
12 284 0.0284 0.027777777777777776
Number of rolls: 100000
Spots Rolls Fractions
2 2786 0.02786 0.027777777777777776
3 5573 0.05573 0.05555555555555555
4 8275 0.08275 0.08333333333333333
5 11155 0.11155 0.1111111111111111
6 13680 0.1368 0.1388888888888889
7 16594 0.16594 0.16666666666666666
8 14068 0.14068 0.1388888888888889
9 11006 0.11006 0.1111111111111111
10 8472 0.08472 0.08333333333333333
11 5595 0.05595 0.05555555555555555
12 2796 0.02796 0.027777777777777776
Number of rolls: 1000000
Spots Rolls Fractions
2 27884 0.027884 0.027777777777777776
3 55640 0.05564 0.05555555555555555
4 83415 0.083415 0.08333333333333333
5 110796 0.110796 0.1111111111111111
6 138871 0.138871 0.1388888888888889
7 166810 0.16681 0.16666666666666666
8 138893 0.138893 0.1388888888888889
9 111246 0.111246 0.1111111111111111
10 83123 0.083123 0.08333333333333333
11 55487 0.055487 0.05555555555555555
12 27835 0.027835 0.027777777777777776
Number of rolls: 10000000
Spots Rolls Fractions
2 277482 0.0277482 0.027777777777777776
3 555857 0.0555857 0.05555555555555555
4 833341 0.0833341 0.08333333333333333
5 1110670 0.111067 0.1111111111111111
6 1390217 0.1390217 0.1388888888888889
7 1666431 0.1666431 0.16666666666666666
8 1387210 0.138721 0.1388888888888889
9 1112055 0.1112055 0.1111111111111111
10 833358 0.0833358 0.08333333333333333
11 555361 0.0555361 0.05555555555555555
12 278018 0.0278018 0.027777777777777776
Number of rolls: 100000000
Spots Rolls Fractions
2 2779255 0.02779255 0.027777777777777776
3 5555448 0.05555448 0.05555555555555555
4 8329070 0.0832907 0.08333333333333333
5 11109572 0.11109572 0.1111111111111111
6 13887906 0.13887906 0.1388888888888889
7 16667675 0.16667675 0.16666666666666666
8 13886650 0.1388665 0.1388888888888889
9 11111288 0.11111288 0.1111111111111111
10 8338265 0.08338265 0.08333333333333333
11 5557585 0.05557585 0.05555555555555555
12 2777286 0.02777286 0.027777777777777776
Revision date: 2003-10-15.
(Please use ISO
8601, the International Standard.)