-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.java
56 lines (48 loc) · 1.23 KB
/
Common.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Common {
static public int s2i (String s) {
int i = 0;
try {
i = Integer.parseInt(s.trim());
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
return i;
}
static public double R1 () {
java.util.Random generator = new java.util.Random(System.currentTimeMillis());
double U = generator.nextDouble();
while (U < 0 || U >= 1) {
U = generator.nextDouble();
}
double V = generator.nextDouble();
while (V < 0 || V >= 1) {
V = generator.nextDouble();
}
double X = Math.sqrt((8/Math.E)) * (V - 0.5)/U;
if (!(R2(X,U))) { return -1; }
if (!(R3(X,U))) { return -1; }
if (!(R4(X,U))) { return -1; }
return X;
}
static public boolean R2 (double X, double U) {
if ((X * X) <= (5 - 4 * Math.exp(.25) * U)) {
return true;
} else {
return false;
}
}
static public boolean R3 (double X, double U) {
if ((X * X) >= (4 * Math.exp(-1.35) / U + 1.4)) {
return false;
} else {
return true;
}
}
static public boolean R4 (double X, double U) {
if ((X * X) < (-4 * Math.log(U))) {
return true;
} else {
return false;
}
}
}