Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 20, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/ec12c60f2d907931a9860cc0e93dcffd to your computer and use it in GitHub Desktop.
Save joriki/ec12c60f2d907931a9860cc0e93dcffd to your computer and use it in GitHub Desktop.
Test results for the expected median of the counts in uniformly randomly filled bins; see http://math.stackexchange.com/questions/1749770.
import java.util.Arrays;
import java.util.Random;
public class Question1749770 {
final static int ntrials = 10000000;
final static int k = 1;
final static int nbins = 2 * k + 1;
final static Random random = new Random ();
public static void main (String [] args) {
int [] bins = new int [nbins];
for (int n = nbins;;n += nbins) {
double sum = 0;
double sum2 = 0;
for (int i = 0;i < ntrials;i++) {
Arrays.fill (bins,0);
for (int j = 0;j < n;j++)
bins [random.nextInt (nbins)]++;
Arrays.sort (bins);
sum += bins [k];
sum2 += bins [k] * bins [k];
}
double mean = sum / ntrials;
double mean2 = sum2 / ntrials;
System.out.println (n + " : " + mean + " ± " + Math.sqrt ((mean2 - mean * mean) / ntrials) + " / " + (n / (double) nbins - 1 / (2 * Math.PI * Math.sqrt (3))));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment