Created
March 9, 2013 01:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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