-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineGame.java
More file actions
30 lines (23 loc) · 823 Bytes
/
LineGame.java
File metadata and controls
30 lines (23 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class LineGame {
public static boolean trial() {
// For the sake of the problem, I'm saying the lines are vertical, bc you can always rotate them
// the y-value of the initial point really doesn't matter, and the x can be modded by l
// So, I guess l = 1 for the sake of this
double x = Math.random();
// To make this point into a random line, I'll choose the other endpoint through getting a random angle
double angle = Math.random() * 2 * Math.PI;
if (x + Math.cos(angle) <= 0 || x + Math.cos(angle) >= 1)
return true;
return false;
}
public static void main(String[] args) {
int trials = 1000000;
int success = 0;
for (int i = 0; i < trials; i++) {
if (trial()) {
success++;
}
}
System.out.println(2 / (success * 1.0 / trials));
}
}