Guest User

Bulk Coin Flip Test for r/incremental_games

a guest
Jan 28th, 2022
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package com.example;
  2.  
  3. import static org.apache.commons.math.util.MathUtils.factorialDouble;
  4.  
  5. public class BulkCoinFlipTest {
  6.  
  7.     private static boolean myChoose = false;
  8.     private static boolean simulate = true;
  9.     private static boolean memoize = false;
  10.     private static boolean printHistogram = false;
  11.  
  12.     private static Double[] chanceMemo = null;
  13.  
  14.     public static void main(String[] args) {
  15.         int periods = 1_000_000;
  16.         double x = 0.2;
  17.         int n = 80;
  18.  
  19.         // Pre-initialize the math stuff, for clean timing
  20.         for (int i = 0; i < n; i++) {
  21.             try {
  22.                 basicXChooseY(n, i);
  23.             } catch (Exception ignore) {}
  24.             myXChooseY(n, i);
  25.         }
  26.  
  27.         simulate = false;
  28.         runTrial(periods, x, n);
  29.  
  30.         simulate = true;
  31. //        myChoose = false;
  32. //        runTrial(periods, x, n);
  33.  
  34.         myChoose = true;
  35.         runTrial(periods, x, n);
  36.  
  37.         memoize = true;
  38.         runTrial(periods, x, n);
  39.     }
  40.  
  41.     private static void runTrial(int periods, double x, int n) {
  42.         try {
  43.             System.out.println(periods + " tests of " + n + " rolls with " + (x * 100) + "% chance of success, using " +
  44.                                        (simulate ? (myChoose ? "my" : "the basic") + " choose function and " + (memoize ? "" : "no ") + "memoization"
  45.                                                  : "individual rolls"));
  46.             if (memoize) chanceMemo = new Double[n];
  47.             int[] hist = new int[n + 1];
  48.             long start = System.currentTimeMillis();
  49.             for (int i = 0; i < periods; i++) {
  50.                 hist[getSuccesses(x, n)]++;
  51.             }
  52.             long elapsed = System.currentTimeMillis() - start;
  53.             System.out.println("Elapsed time (ms): " + elapsed);
  54.             if (printHistogram) printHistogram(hist, periods);
  55.         } catch (Exception e) {
  56.             System.out.println(e.toString());
  57.         }
  58.         System.out.println();
  59.     }
  60.  
  61.     static int getSuccesses(double x, int n) {
  62.         return simulate ? getSimulatedSuccesses(x, n) : getRealSuccesses(x, n);
  63.     }
  64.  
  65.     static int getSimulatedSuccesses(double x, int n) {
  66.         double roll = Math.random();
  67.         int result = 0;
  68.         double chance = getChanceOfMSuccesses(x, n, result);
  69.         while (chance < roll) {
  70.             roll -= chance;
  71.             result++;
  72.             chance = getChanceOfMSuccesses(x, n, result);
  73.         }
  74.         return result;
  75.     }
  76.  
  77.     static int getRealSuccesses(double x, int n) {
  78.         int result = 0;
  79.         for (int i = 0; i < n; i++) {
  80.             if (Math.random() < x) result++;
  81.         }
  82.         return result;
  83.     }
  84.  
  85.     static double getChanceOfMSuccesses(double x, int n, int m) {
  86.         if (memoize) {
  87.             if (chanceMemo[m] != null) return chanceMemo[m];
  88.             return chanceMemo[m] = Math.pow(x, m) * Math.pow(1 - x, n - m) * xChooseY(n, m);
  89.         } else {
  90.             return Math.pow(x, m) * Math.pow(1 - x, n - m) * xChooseY(n, m);
  91.         }
  92.     }
  93.  
  94.     static double xChooseY(int x, int y) {
  95.         if (myChoose) return myXChooseY(x, y);
  96.         else return basicXChooseY(x, y);
  97.     }
  98.  
  99.     static double basicXChooseY(int x, int y) {
  100.         if (x > 170 || y > 170) throw new IllegalArgumentException("Too large for factorial calculation");
  101.         return factorialDouble(x) / (factorialDouble(y) * factorialDouble(x - y));
  102.     }
  103.  
  104.     static double myXChooseY(int x, int y) {
  105.         if (2*y >= x) {
  106.             double result = 1;
  107.             for (int i = x; i > y; i--) {
  108.                 result *= i;
  109.             }
  110.             return result / factorialDouble(x-y);
  111.         } else {
  112.             return myXChooseY(x, x-y);
  113.         }
  114.     }
  115.  
  116.     // Useful for comparing results between methods for correctness
  117.     static void printHistogram(int[] hist, int n) {
  118.         System.out.println("Successes: periods\n");
  119.         int digits = new Double(Math.log10(n)).intValue() + 1;
  120.         int idigits = new Double(Math.log10(hist.length)).intValue() + 1;
  121.         for (int i = 0; i < hist.length; i++) {
  122.             System.out.printf("%" + idigits + "d: %" + digits + "d\n", i, hist[i]);
  123.         }
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment