Skip to content

Instantly share code, notes, and snippets.

@n-west
Created January 8, 2013 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n-west/4487666 to your computer and use it in GitHub Desktop.
Save n-west/4487666 to your computer and use it in GitHub Desktop.
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