Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 9, 2013 10:27
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/5344687 to your computer and use it in GitHub Desktop.
Save joriki/5344687 to your computer and use it in GitHub Desktop.
Calculate the average area of a lune determined by two points uniformly randomly picked in a disk under the condition that the lune contains the origin; see http://math.stackexchange.com/questions/355415.
public class Question355415 {
public static void main (String [] args) {
int ntrials = Integer.parseInt (args [0]);
double sum = 0;
double [] [] p = new double [3] [2];
for (int n = 0;n < ntrials;n++) {
double d;
do {
for (int i = 0;i < 2;i++) {
double s;
do {
s = 0;
for (int j = 0;j < 2;j++) {
p [i] [j] = 2 * Math.random () - 1;
s += p [i] [j] * p [i] [j];
}
} while (s > 1);
}
d = d (p,0,1);
} while (d < d (p,0,2) || d < d (p,1,2));
sum += d;
}
System.out.println (sum / ntrials);
}
static double d (double [] [] p,int i1,int i2) {
double d = 0;
for (int j = 0;j < 2;j++) {
double diff = p [i1] [j] - p [i2] [j];
d += diff * diff;
}
return d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment