Skip to content

Instantly share code, notes, and snippets.

@joriki
Created February 8, 2024 08:26
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/4dd7f405bf4b7967c026f335350e8cf1 to your computer and use it in GitHub Desktop.
Save joriki/4dd7f405bf4b7967c026f335350e8cf1 to your computer and use it in GitHub Desktop.
Estimate the probability of a certain inequality being satisfied for uniformly distributed random variables; see https://math.stackexchange.com/questions/4859023.
public class Question4859023 {
final static long ntrials = 100000000L;
public static void main(String [] args) {
long count = 0;
for (long n = 0;n < ntrials;n++) {
double x1 = 5 * Math.random();
double x2 = 5 * Math.random();
double y1 = 5 * Math.random();
double y2 = 5 * Math.random();
if ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) > (x2 + x1) * (x2 + x1))
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