Advertisement
Guest User

Untitled

a guest
May 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.Set;
  7.  
  8. /*
  9. Checks the chance of having duplicate elements, when getting random numbers between 1 and 14. Both mathematical and experimental chances (and
  10. their differences) are printed. java.util.Random is used for getting random numbers.
  11. */
  12. public class RelicDuplicate {
  13.  
  14. static List<Double> math = new ArrayList<>();
  15. static List<Double> exp = new ArrayList<>();
  16. static final double NUMBER_OF_TRIES = Math.pow(10, 8); //100 million
  17.  
  18. public static void main(String[] args) {
  19. mathematical();
  20. experimental();
  21.  
  22. showChanceDifference();
  23. }
  24.  
  25. /*
  26. Chance of NOT having duplicate values in N distinct values with K tries:
  27. P = K! * (N choose K) / N^K = (N!/(N-K)!)/N^K
  28. Chance of HAVING duplicate values: 1 - P
  29. */
  30. public static void mathematical() {
  31. double chance;
  32. for (int i = 1; i <= 14; i++) {
  33. chance = 1 - ((factorialOf(14) / factorialOf(14 - i)) / Math.pow(14, i));
  34. math.add(chance);
  35. System.out.println("MATHEMATICAL CHANCE AT ROLL " + i + " FOR DUPLICATE: " + chance);
  36. }
  37. }
  38.  
  39. public static void experimental() {
  40. Random r = new Random();
  41. int low = 1;
  42. int high = 14;
  43.  
  44. List<Integer> duplicateIndexes = new ArrayList<>();
  45. Set<Integer> oneRoundResults = new HashSet<>();
  46.  
  47. for (int i = 0; i < NUMBER_OF_TRIES; i++) {
  48. for (int j = 1; j <= 14; j++) {
  49. // nextInt(bound) returns numbers between 0 (inclusive) and bound (exclusive), so with this, we get numbers between 1 and 14
  50. int relic = r.nextInt(high - low) + low;
  51. oneRoundResults.add(relic);
  52.  
  53. /* Sets can not have duplicate elements, trying to add one will have no effect. So if the size of the set is less than j, we had a duplicate
  54. element.
  55. */
  56. if (oneRoundResults.size() < j) {
  57. duplicateIndexes.add(j);
  58. break;
  59. }
  60. }
  61.  
  62. oneRoundResults.clear();
  63. }
  64.  
  65. double sum = 0.0;
  66. double frequency;
  67. double frequencySum = 0.0;
  68. for (int i = 1; i <= 14; i++) {
  69. frequency = (double) Collections.frequency(duplicateIndexes, i);
  70. frequencySum += frequency;
  71. sum += (frequency / duplicateIndexes.size());
  72. exp.add(sum);
  73. System.out.println("EXPERIMENTAL CHANCE AT ROLL " + i + ": " + sum + " (with frequnce of " + frequency + " out of " + NUMBER_OF_TRIES + ")");
  74. }
  75.  
  76. if (frequencySum != NUMBER_OF_TRIES) {
  77. System.out.println("The experimental part has a bug. Frequency sum: " + frequencySum);
  78. }
  79. }
  80.  
  81. public static void showChanceDifference() {
  82. for (int i = 0; i < 14; i++) {
  83. System.out.println("Experimental chance - mathematical chance at roll " + (i + 1) + ": " + String.valueOf(exp.get(i) - math.get(i)));
  84. }
  85. }
  86.  
  87. public static double factorialOf(int a) {
  88. if (a < 0) {
  89. throw new IllegalArgumentException("cheese");
  90. }
  91. if (a == 0) {
  92. return 1;
  93. }
  94. double factorial = 1.0;
  95. for (int i = 2; i <= a; i++) {
  96. factorial *= i;
  97. }
  98.  
  99. return factorial;
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement