?? kids.m
字號:
%kids.m/created by PJNahin for "Duelling Idiots"(11/12/98)
%This m-file simulates 10,000 families that have children
%until a child is born that is the same sex as the first
%child, where p is the probability of a boy.
%
%
rand('state',100*sum(clock)) %set new generator seed
p=input('What is the probability of a boy? ');
total=0; %total number of children in 10,000 families;
s=0; %number of children in present family;
for family=1:10000
c=rand; %generate first child;
s=s+1;
if c<p
first=0; %first=0 if first child is a boy;
else
first=1; %first=1 if first child is a girl;
end
while s>0 %generate another child;
c=rand;
s=s+1;
if c<p
if first==0 %end of family growth if new
total=total+s; %child matches the first child
s=0; %in sex, i.e., is a boy;
else
end
end
if c>p
if first==1 %ditto for a girl;
total=total+s;
s=0;
else
end
end
end
end
total/10000 %print average number of children in
%a family;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -