Untitled
unknown
plain_text
9 months ago
854 B
11
Indexable
function p_stay = monty_stay(n, k)
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