-
Notifications
You must be signed in to change notification settings - Fork 173
/
MonteCarloExperiment.java
52 lines (36 loc) · 1.31 KB
/
MonteCarloExperiment.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
import java.awt.*;
public class MonteCarloExperiment {
private int squareSide;
private int N;
private int outputInterval = 100;
public MonteCarloExperiment(int squareSide, int N){
if(squareSide <= 0 || N <= 0)
throw new IllegalArgumentException("squareSide and N must larger than zero!");
this.squareSide = squareSide;
this.N = N;
}
public void setOutputInterval(int interval){
if(interval <= 0)
throw new IllegalArgumentException("interval must be larger than zero");
this.outputInterval = interval;
}
public void run(){
Circle circle = new Circle(squareSide/2, squareSide/2, squareSide/2);
MonteCarloPiData data = new MonteCarloPiData(circle);
for(int i = 0 ; i < N ; i ++){
if( i % outputInterval == 0) {
System.out.println(data.estimatePi());
}
int x = (int)(Math.random() * squareSide);
int y = (int)(Math.random() * squareSide);
data.addPoint(new Point(x, y));
}
}
public static void main(String[] args) {
int squareSide = 800;
int N = 10000000;
MonteCarloExperiment exp = new MonteCarloExperiment(squareSide, N);
exp.setOutputInterval(100000);
exp.run();
}
}