% MATLAB program to simulate "Let's Make a Deal" with Monty Hall.
% Requires Statistics Toolbox.
%
% Rich Kozick, ELEC 471, Spring 1999

RUNs = 100;   % Number of trials

win_keep = 0;
win_change = 0;
win_rand = 0;

for n=1:RUNs

prize = floor(unifrnd(1, 4, 1, 1));
guess = floor(unifrnd(1, 4, 1, 1));

keep = guess;

if (prize == guess)   % Assume Monty always shows you the lower number curtain
  if (prize == 1)
    change = 3;
  elseif (prize == 2)
    change = 3;
  elseif (prize == 3)
    change = 2;
  end
else
  if (prize == 1 & guess == 2)
    change = 1;
  elseif (prize == 1 & guess == 3)
    change = 1;
  elseif (prize == 2 & guess == 1)
    change = 2;
  elseif (prize == 2 & guess == 3)
    change = 2;
  elseif (prize == 3 & guess == 1)
    change = 3;
  elseif (prize == 3 & guess == 2)
    change = 3;
  end
end

% If randomly decide between keeping first choice and changing
random_decide = floor(unifrnd(1,3,1,1));
if (random_decide == 1)
  decide = keep;
else
  decide = change;
end

if (keep == prize)
  win_keep = win_keep + 1;
end
if (change == prize)
  win_change = win_change + 1;
end
if (decide == prize)
  win_rand = win_rand + 1;
end

end

fprintf('If always switch:     %4d wins in %5d runs\n', win_change, RUNs);
fprintf('If always keep:       %4d wins in %5d runs\n', win_keep, RUNs);
fprintf('If randomly decide:   %4d wins in %5d runs\n', win_rand, RUNs);

