-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCPI.java
39 lines (28 loc) · 876 Bytes
/
MCPI.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
import java.util.Random;
public class MCPI {
public final static long ITERATIONS = 1000*1000000;
private long _iterations;
public MCPI(long iterations) {
this._iterations = iterations;
}
public double value() {
Random r = new Random();
long pi = 0;
for (int i = 1; i < _iterations; i++) {
double x = r.nextDouble();
double y = r.nextDouble();
double d = x*x + y*y;
if (d < 1) pi++;
if(0 == i%(ITERATIONS/10)) {
System.out.println("x=" + x + "y=" + y + " r=" + d);
System.out.println("(intermediary) PI with " + i + " iteration is " + ((double)pi/i)*4);
}
}
return (double)pi/_iterations*4;
}
public static void main(String[] args) {
MCPI _i = new MCPI(ITERATIONS);
double PI = _i.value();
System.out.println("PI with " + ITERATIONS + " iteration is " + PI);
}
}