thief.m

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

M
31
字号
%thief.m/created by PJNahin for "Duelling Idiots"(11/14/98)
%This m-file simulates "The Thief of Baghdad" problem for
%10,000 thieves.
%
%
rand('state',100*sum(clock))      %set new generator seed;
long=3;                           %time duration of long tunnel;
short=1;                          %time duration of short tunnel;
total=0;                          %total prison time for all thieves;
p1=1/3;
p2=2/3;
for thiefn=1:10000
   time=0;                        %initialize prison time for current
                                  %thief;
   trytoescape=1;                 %set flag for thief about to choose
                                  %a door for the first time;
     while trytoescape>0
        door=rand;
        if door<p1                %thief escapes,
           total=total+time;      %so up-date total prison time;
           trytoescape=0;         %reset flag to break while loop;
        elseif door<p2            %thief picks short tunnel and so
           time=time+short;       %up-date current thief's prison time;
        else
           time=time+long;        %thief picks long tunnel;
        end
     end
  end
  total/10000                     %print average time in prison
                                     
   

⌨️ 快捷键说明

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