Code covered by the BSD License  

Highlights from
Coin And Dice

from Coin And Dice by Rodolphe Sitter
Simulate the average number of tosses of coin/throws of dice to get a given sequence.

ThrowDice(Seq,N)
function ThrowDice(Seq,N)
%THROWDICE 
%   Compute the estimated average number of dice throws to get a given
%   sequence.
%
%   ThrowDice(Seq,N)
%
%==========================================================================
%   INPUTS:
%
%   Seq   	- Sequence (row vector) of integers ranging from 1 to 6.
%
%   N       - Number of Monte-Carlo Simulations
%
%==========================================================================
%   EXAMPLE:
% >>   ThrowDice([6,6],10^4)
%
% >>   OUTPUT:
%      THE SEQUENCE IS: 
%            6     6
%      The average number of throwing the dice to get this sequence is:  42  
%      (Sample Mean= 41.61 standard deviation=  0.40)
%
%**************************************************************************
% Rodolphe Sitter - MSFM Student - The University of Chicago
% March 27, 2009
%**************************************************************************

M=length(Seq);
C=round(6*rand(M,N)+.5);
Count=zeros(N,1);

for j=1:N  
    c=M;
    while min(C(:,j)==Seq')==0
        C(1:M-1,j)=C(2:M,j);
        C(M,j)=round(6*rand(1,1)+.5);
        c=c+1;
    end
    Count(j)=c;    
end

Mean=mean(Count);
Throws=round(Mean);
STD_err=std(Count)/sqrt(N);
fprintf('\n==============================================================')
fprintf('\n          DICE THROWING BY MONTE-CARLO SIMULATION          \n\n')
disp('THE SEQUENCE IS: ')
disp(Seq)
fprintf('The average number of throwing the dice to get this sequence is:%4.0f  \n',Throws);
fprintf('(Sample Mean=%6.2f standard deviation=%6.2f)\n\n',Mean,STD_err);

Contact us at files@mathworks.com