Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. //Tai Lum
  2.  
  3. public class ProblemSet1 {
  4. public static void main(String[] args) {
  5. int sum;
  6. int SIDES = 6;
  7. int t = 0;
  8. int dice1;
  9. int dice2;
  10. int numRolls = 500000;
  11. double[] distcalc = new double[2 * SIDES + 1];
  12. //Start of Experimental Data Collection of 500000 rolls of the dice
  13. while (t < numRolls) {
  14. dice1 = (int) (Math.random() * 6 + 1);
  15. dice2 = (int) (Math.random() * 6 + 1);
  16. sum = dice1 + dice2;
  17. distcalc[sum] += 1.0;
  18. t++;
  19. sum = 0;
  20. }
  21.  
  22. System.out.println("Experimental Data");
  23.  
  24. for (int k = 2; k <= 2 * SIDES; k++) {
  25. distcalc[k] /= numRolls;
  26. System.out.println(distcalc[k]);
  27. }
  28.  
  29. System.out.println("");
  30. System.out.println("Empirical Data");
  31.  
  32. double[] dist = new double[2 * SIDES + 1];
  33. for (int i = 1; i <= SIDES; i++) {
  34. for (int j = 1; j <=SIDES; j++) {
  35. dist[i + j] += 1.0;
  36. }
  37. }
  38.  
  39. for (int k = 2; k <= 2 * SIDES; k++) {
  40. dist[k] /= 36.0;
  41. System.out.println(dist[k]);
  42. }
  43.  
  44. System.out.println(compareValues(dist, distcalc));
  45. }
  46.  
  47. // Comparison method to check if the number of rolls was enough
  48. public static boolean compareValues(double[] a, double[] b) {
  49. double comparisonValue = 0.001;
  50. for (int i = 2; i <= 12; i++) {
  51. double difference = Math.abs(a[i] - b[i]);
  52. if (difference > comparisonValue)
  53. return false;
  54.  
  55. }
  56. return true;
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement