Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tai Lum
- public class ProblemSet1 {
- public static void main(String[] args) {
- int sum;
- int SIDES = 6;
- int t = 0;
- int dice1;
- int dice2;
- int numRolls = 500000;
- double[] distcalc = new double[2 * SIDES + 1];
- //Start of Experimental Data Collection of 500000 rolls of the dice
- while (t < numRolls) {
- dice1 = (int) (Math.random() * 6 + 1);
- dice2 = (int) (Math.random() * 6 + 1);
- sum = dice1 + dice2;
- distcalc[sum] += 1.0;
- t++;
- sum = 0;
- }
- System.out.println("Experimental Data");
- for (int k = 2; k <= 2 * SIDES; k++) {
- distcalc[k] /= numRolls;
- System.out.println(distcalc[k]);
- }
- System.out.println("");
- System.out.println("Empirical Data");
- double[] dist = new double[2 * SIDES + 1];
- for (int i = 1; i <= SIDES; i++) {
- for (int j = 1; j <=SIDES; j++) {
- dist[i + j] += 1.0;
- }
- }
- for (int k = 2; k <= 2 * SIDES; k++) {
- dist[k] /= 36.0;
- System.out.println(dist[k]);
- }
- System.out.println(compareValues(dist, distcalc));
- }
- // Comparison method to check if the number of rolls was enough
- public static boolean compareValues(double[] a, double[] b) {
- double comparisonValue = 0.001;
- for (int i = 2; i <= 12; i++) {
- double difference = Math.abs(a[i] - b[i]);
- if (difference > comparisonValue)
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement