24小时热门版块排行榜    

查看: 490  |  回复: 1
【奖励】 本帖被评价1次,作者argo增加金币 0.5
当前主题已经存档。

argo

铁杆木虫 (著名写手)


[资源] 【转贴】《蒙卡入门篇》的程序代码

原贴在:
http://muchong.com/bbs/viewthread.php?tid=589636&fpage=1&view=&highlight=&page=1

这么小的程序代码用附件,太浪费大家的金币了。我下了,代码贴在下面,希望没有违反版规。

Example C Program to Compute PI Using A Monte Carlo Method

Source code:

/* Program to compute Pi using Monte Carlo methods */

#include
#include
#include
#include
#define SEED 35791246

main(int argc, char* argv)
{
   int niter=0;
   double x,y;
   int i,count=0; /* # of points in the 1st quadrant of unit circle */
   double z;
   double pi;

   printf("Enter the number of iterations used to estimate pi: ";
   scanf("%d",&niter);

   /* initialize random numbers */
   srand(SEED);
   count=0;
   for ( i=0; i       x = (double)rand()/RAND_MAX;
      y = (double)rand()/RAND_MAX;
      z = x*x+y*y;
      if (z<=1) count++;
      }
   pi=(double)count/niter*4;
   printf("# of trials= %d , estimate of pi is %g \n",niter,pi);
}

[ Last edited by sunxiao on 2009-3-10 at 05:17 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Delphi001

铜虫 (小有名气)


★★★ 三星级,支持鼓励

好像编译不能通过,不过还是可以参考,谢谢
2楼2009-03-11 17:13:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 argo 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见