Advertisement
Guest User

Untitled

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