Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 17, 2020 05:38
Show Gist options
  • Save joriki/243e9636606747357558d15589b83e74 to your computer and use it in GitHub Desktop.
Save joriki/243e9636606747357558d15589b83e74 to your computer and use it in GitHub Desktop.
Simulate the angle between two vectors uniformly randomly picked in the unit square; see https://math.stackexchange.com/questions/3629474.
import java.util.Random;
public class Question3629474 {
final static long ntrials = 100000000;
final static Random random = new Random();
public static void main(String [] args) {
double sum = 0;
for (long n = 0;n < ntrials;n++)
sum += Math.abs(angle() - angle());
System.out.println(sum / ntrials);
}
static double angle() {
return (Math.atan2(random.nextDouble(), random.nextDouble()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment