蒲丰投针实验
下面是一个关于蒲丰投针实验的c程序,不过关于角度和距离的随机我觉得我做的还不是很好,数据拟合的比较勉强,希望有大大能帮忙提提建议,谢谢。#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define M 1000000
int main(void)
{
int i,count=0;
float d=2.0,l=1.6,p,x,a;
for(i=1;i<=M;i++)
{
a=(rand()%315)*0.01;
x=(rand()%1001)*0.001;
if(x<=(l/2*sin(a)))
count++;
}
printf("the count is %d ,probability is %lf\n",count,(double)count/M);
printf("the pi is near %f\n",(float)(2*l*M)/(d*count));
return 0;
}