|
C语言趣味程序百例精解之java实现:
79.随机数求圆周率
public class Test79{
public static void main(String args[]){
getPaiByRandom79();
}
/**
* 79。随机数求圆周率
*/
public static void getPaiByRandom79() {
double all = 10000000;
double l = 1000000;
double pai = 0;
double in = 0;
double x = 0, y = 0;
for (int i = 0; i < all; i++) {
x = (Math.random() * l);
y = (Math.random() * l);
if (x * x + y * y < l * l)
in++;
}
pai = (in / all * 4);
System.out.println("Times/All=" + in + "/" + all + " pai~=" + pai);
}
}
运行:
C:\bat>java Test79
Times/All=7852808.0/1.0E7 pai~=3.1411232 |
|