function winProb = MontyHall(Keep)
% This function calculates the probability of winning in the Monty Hall
% game. If argument "Keep" equals one, the assumed strategy is to keep the
% door choice, while if "Keep" equals zero, the strategy is to switch.

nTrials = 100000;
nWins = 0;

for t = 1:nTrials
    Prize = ceil(3*rand);    % Door with the prize
    Choice = ceil(3*rand);   % Door which is chosen
    Open = 1;                % Loop below determines which door will be opened
    while (Open == Choice || Open == Prize) Open = Open + 1;
    end;
    if (Keep == 1 && Prize == Choice) || (Keep == 0 && Prize ~= Choice)
        nWins = nWins + 1;   % If the strategy worked, increment "nWins"
    end
end

winProb = nWins / nTrials
            
