#include <iostream> using namespace std; //概率计算PI int main() { int inside=0; double val; int i; for ( i=0; i<100000000; i++) { double x = (double)(rand())/RAND_MAX; double y = (double)(rand())/RAND_MAX; if ( (x*x + y*y) <= 1.0 ) { inside++; } } val = (double)inside / i; printf("PI = %.4g\n", val*4); return 0; }
原文:http://www.cnblogs.com/Yogurshine/p/3705491.html