Skip to content

Instantly share code, notes, and snippets.

@joriki
Created March 9, 2013 01:30
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/5122036 to your computer and use it in GitHub Desktop.
Save joriki/5122036 to your computer and use it in GitHub Desktop.
Estimate the probability for n points chosen in/on a circle to lie in/on some semicircle; see http://math.stackexchange.com/questions/325141.
import java.util.Arrays;
public class Question325141 {
public static void main (String [] args) {
int ntrials = Integer.parseInt (args [0]);
int n = Integer.parseInt (args [1]);
double [] a = new double [n];
int count = 0;
for (int i = 0;i < ntrials;i++) {
for (int j = 0;j < n;j++)
a [j] = Math.random ();
Arrays.sort (a);
for (int j = 0;j < n;j++) {
double diff = a [j] - a [(j + 1) % n];
if (diff < 0)
diff++;
if (diff < 0.5) {
count++;
break;
}
}
}
System.out.println (count / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment