pisim.m

来自「一些常被用于教学或者参考的概率论的实例的源代码」· M 代码 · 共 21 行

M
21
字号
%pisim.m/created by PJNahin for "Duelling Idiots"(10/22/98)
%This m-file estimates pi by randomly tossing darts at the
%unit square and counting how many land inside a quarter-
%circle with unit radius contained in the square.
%
%
rand('state',100*sum(clock))  %set new seed for generator;
darts=0;                      %initialize number of darts
                              %inside quarter-circle region;
for i=1:10000;
   x=rand;             %toss a
   y=rand;             %dart;
   r=x*x+y*y;          %compute distance squared from origin to dart;
   if r<1              %is dart inside quarter-circle region?
      darts=darts+1;   %yes
   end
end
pi_estimate=4*darts/10000
   
                                                              

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?