📄 idiots1.m
字号:
%idiots1.m/created by PJNahin for "Duelling Idiots"(7/3/98)
%This m-file simulates the original dueling idiots. The elements
%of the row vector duration are the number of duels of length k,
%i.e., duration(k)=# of duels that require k trigger-pulls to
%complete, where k=1,2,3,...
%
%
Duels=10000; %total number of duels;
duration=zeros(1,60); %assume no duel exceeds 60 trigger-pulls;
a=0; %number of times A has won, so far;
rand('state',100*sum(clock)) %new seed for generator
nd=0; %number of duels completed, so;
tp=0; %number of trigger pulls, so far, in present duel
while nd<Duels
ra=rand; %get two random numbers
rb=rand;
tp=tp+1; %A pulls the trigger
if (ra<=1/6) %A wins
duration(tp)=duration(tp)+1; %duel is over, up-date the
%duration vector;
tp=0; %initialize number of trigger-pulls for
%the next duel;
a=a+1;
nd=nd+1;
else
tp=tp+1; %B pulls the trigger
if (rb<=1/6) %B wins
duration(tp)=duration(tp)+1; %duel is over, up-date the
%duration vector;
tp=0; %initialize the number of
%trigger-pulls for the
%next duel;
nd=nd+1;
end
end
end %start next duel;
a=a/Duels;
disp(['The probability A wins is ',num2str(a)])
average=0;
for k=1:length(duration)
average=average+k*duration(k);
end
average=average/Duels;
disp(['The average number of trigger-pulls/duel is 'num2str(average)])
bar(duration) %plot a bar graph of the duration vector
title('Figure 2.1 - Relative Frequency of the Number of Trigger-Pulls per Duel')
xlabel('duration of duels, in units of trigger-pulls')
ylabel('number of duels')
figure(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -