-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIByMonte.java
More file actions
31 lines (30 loc) · 846 Bytes
/
Copy pathPIByMonte.java
File metadata and controls
31 lines (30 loc) · 846 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
public class PIByMonte {
private double axis_x = 0.0;
private double axis_y = 0.0;
private double n = 0;
private long m = 0;
PIByMonte(long n)
{
this.n = n;
}
public void Simulation()
{
for(int i=1;i<n+1;i++)
{
axis_x = Math.random();
axis_y = Math.random();
if(Math.sqrt(axis_x*axis_x+axis_y*axis_y)<1){
m++;
}
}
System.out.printf("PI is about %.7f\n",4*m/n);
}
public static void main(String[] args) {
PIByMonte simulation = new PIByMonte(100);
simulation.Simulation();
PIByMonte simulation2 = new PIByMonte(100000);
simulation2.Simulation();
PIByMonte simulation3 = new PIByMonte(10000000);
simulation3.Simulation();
}
}