Created
April 17, 2020 05:38
-
-
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.
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.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