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

RUNs = 100;   % Number of trials

win_keep = 0;     % Counts number of wins when keep initial guess
win_change = 0;   % Counts number of wins when always change (or switch)
win_rand = 0;     % Counts number of wins if randomly keep/switch

for n=1:RUNs

prize = floor(unifrnd(1, 4, 1, 1));  % Choose a curtain for prize : 1, 2, or 3
guess = floor(unifrnd(1, 4, 1, 1));  % Contestant's initial choice

keep = guess;

if (prize == guess) % Contestant chose the "prize" on the initial guess
  if (prize == 1)   % Assume Monty always shows you the lower number curtain
    change = 3;
  elseif (prize == 2)
    change = 3;
  elseif (prize == 3)
    change = 2;
  end
else                % Contestant chose bad prize on initial guess
  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));    % Either 1 or 2, with equal prob.
if (random_decide == 1)
  decide = keep;
else
  decide = change;
end

if (keep == prize)          % Increment the appropriate win counters
  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);

