#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;
}
时间: 2024-12-22 13:03:19