⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kids.m

📁 一些常被用于教学或者参考的概率论的实例的源代码
💻 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 + -