Untitled

 avatar
unknown
plain_text
9 months ago
1.3 kB
12
Indexable
function p_stay = monty_stay(n, k)
% MONTY_STAY  Estimate win probability for "stay" strategy in generalized Monty Hall
%   p_stay = monty_stay(n, k)
%
% Inputs:
%   n - total number of doors (n >= 3)
%   k - number of doors the host opens (1 <= k <= n - 2)
%
% The script runs 1e6 random trials and returns the estimated win probability
% using the STAY strategy.
%
% Hint: uses randi and randperm as requested in the problem.

N = 1e6;           % number of trials
wins = 0;          % counter for wins

for t = 1:N
    % Step 1: Randomly assign the prize door
    prize = randi(n);

    % Step 2: Player makes an initial random choice
    choice = randi(n);

    % Step 3: Host opens k goat doors
    doors = 1:n;
    possible_doors = doors(doors ~= choice & doors ~= prize);  % doors that can be opened
    host_opens = possible_doors(randperm(length(possible_doors), k)); %#ok<NASGU>

    % Step 4: Stay strategy → player keeps original choice
    if choice == prize
        wins = wins + 1;  % win if original choice was prize
    end
end

% Step 5: Estimate probability
p_stay = wins / N;

fprintf('Estimated P(win | stay) for n=%d, k=%d: %.6f\n', n, k, p_stay);
fprintf('Theoretical value = %.6f\n', 1/n);
end
Editor is loading...
Leave a Comment