题目:利用随机数法求π的近似值
/*79.cpp
利用随机数法求π的近似值。
圆心在原点,位于第一象限的1/4圆,求在一点范围内的
大量随机点落于在这1/4的圆内的概率。来估算π。
by as1138 2012-10-14
*/
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
#define MAXNUM 100
#define LOOPNUM 30000
int main(int argc, char const *argv[])
{
int X,Y;
int num = 0;
srand(time(NULL));
for (int i = 0; i < LOOPNUM; ++i)
{
X = rand()%MAXNUM;
Y = rand()%MAXNUM;
if (X*X+Y*Y<10000)
{
num++;
}
}
cout.precision(7);
cout<<"π="<<((double)num/LOOPNUM)*4<<endl;
return 0;
}