-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise4_44.java
More file actions
34 lines (30 loc) · 890 Bytes
/
Exercise4_44.java
File metadata and controls
34 lines (30 loc) · 890 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
31
32
33
34
public class Exercise4_44 {
public static void main(String[] args) {
final int NUMBER_OF_TRIALS = 10000000;
int numberOfHits = 0;
for (int i = 0; i < NUMBER_OF_TRIALS; i++)
{
double x = Math.random() * 2.0 - 1;
double y = Math.random() * 2.0 - 1;
if (x < 0 || isInRegion3(x, y))
numberOfHits++;
}
System.out.println("The probability in Region 1 and 3 is " +
(1.0 * numberOfHits / NUMBER_OF_TRIALS));
}
static boolean isInRegion3(double x, double y) {
if (x > 1 || x < 0 || y > 1 || y < 0)
return false;
else {
/* why do this?
double slope = (1.0 - 0) / (0 - 1.0); //slope is -1.0
double x1 = x + -y * slope; // judge x + y <= 1
*/
double x1 = x + y;
if (x1 <= 1)
return true;
else
return false;
}
}
}