Skip to content

Instantly share code, notes, and snippets.

@joriki
Created November 3, 2012 12:05
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/4007189 to your computer and use it in GitHub Desktop.
Save joriki/4007189 to your computer and use it in GitHub Desktop.
On a randomly uniformly segmented circle, find the measure of the circle lying in segments that are smaller than at least one of the adjacent segments; see http://math.stackexchange.com/questions/227990
import java.util.Arrays;
public class Question227990 {
final static int nbirds = 5;
final static int ntrials = 10000000;
static double [] birds = new double [nbirds];
static double length (int index) {
double length = birds [(index + nbirds) % nbirds] - birds [(index + nbirds - 1) % nbirds];
return length < 0 ? length + 1 : length;
}
public static void main (String [] args) {
int count = 0;
for (int n = 0;n < ntrials;n++) {
for (int i = 0;i < nbirds;i++)
birds [i] = Math.random ();
Arrays.sort (birds);
double point = Math.random ();
int bird;
for (bird = 0;bird < nbirds && point > birds [bird];bird++)
;
if (length (bird) < length (bird - 1) || length (bird) < length (bird + 1))
count++;
}
System.out.println (count / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment