casino.m

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

M
38
字号
%casino.m/created by PJNahin for "Duelling Idiots"(12/27/98)
%This m-file simulates 10,000 games of "Chuck-a-Luck" and
%calculates the average winnings. That is, you bet $1 each
%game and the winnings (W) are how much MORE money you have
%over and above the $10,000 betting money. The average
%winnings are, of course, W/10,000.
%
%
rand('state',100*sum(clock))       %new seed for generator;
W=0;                               %initialize total winnings;
for games=1:10000                  %play 10,000 games;
   bet=floor(6*rand)+1;            %pick an integer from 1 to 6;
   for roll=1:3                    %roll three dice
      die1=floor(6*rand)+1;        %and determine
      die2=floor(6*rand)+1;        %what face shows
      die3=floor(6*rand)+1;        %on each;
   end
   matches=0;                      %initialize number of dice
                                   %that match your number;
   if bet==die1
      matches=matches+1;
   end
   if bet==die2
      matches=matches+1;
   end
   if bet==die3
      matches=matches+1;
   end
   if matches==0                   %if no matches, you
      W=W-1;                       %lose your $1 bet,
   else                            %but with matches you
      W=W+matches;                 %win;
   end
end
W=W/10000;
disp(['Average winnings per game is ',num2str(W),' dollars'])

                                      

⌨️ 快捷键说明

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