Created
January 8, 2013 20:32
-
-
Save n-west/4487666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
totalDays = 0; | |
nTrials = 100000; | |
for null = 1 : nTrials | |
days = 0; % he starts in jail on day 0 | |
free = 0; % he is not free | |
% inner loop is one prison term | |
while(~free) | |
% choose a door | |
door = floor(rand(1)*3); % uniformly distributed random number [0,2] | |
if door == 0 | |
days = days + 1; | |
elseif door == 1 | |
days = days + 3; | |
else % door must be 2 | |
free = 1; % he is free, end | |
end | |
end % while(~free) | |
totalDays = totalDays + days; | |
end %for null = 1 : nTrials | |
fprintf('On average, a thief is in prison for %i days\n', totalDays/nTrials) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment