模拟自然现象求得圆周率近似值
public static void main(String[] args) {
System.out.println(add_p());
}
private static double add_p() {
Random rd=new Random();
int x=0,y=0;
for (int i=0;i<999;i++){
double a=(double) rd.nextInt(100)/100;
double b=(double)rd.nextInt(100)/100;
if((a+b)>1&&(a*a+b*b)<1){
x++;
}y++;
}
return x*4.0/y+2.0;
}