-
-
Save KevinKParsons/efcb2c46138df970a2e04490e41b334b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Program: | |
% three-doors-game-show.m | |
% Three doors game show stastical simulation. | |
% | |
% Simulates a game show where the contestant attempts to pick the correct | |
% one of three doors. The program shows the statistical odds of winning | |
% based on two strategies. The first strategy is to not switch doors when | |
% given the opportunity. The second strategy is to always switch doors when | |
% given the opportunity. | |
% | |
% Variable List: | |
% Always_Switch_Doors = Strategy switch | |
% Iterations = Number of times to simulate | |
% Run = Current run number | |
% Door_Setup = Number to determine door setup | |
% Door = Current door setup | |
% Door_Chosen = Door chosen by contestant | |
% Door_Removed = Door removed by host | |
% Wins = Total number of wins | |
% Losses = Total number of losses | |
% Percent = Percentage of games won | |
% n = Counter | |
clear, clc % Clear command window and workspace | |
Always_Switch_Doors = 1; % Always switch doors? | |
Iterations = 1000; % Number of iterations | |
Wins = 0; % Initialize wins | |
for Run = 1:Iterations % Iterations to run | |
fprintf('Run = %i \n', Run); | |
Door_Setup = randi([1,3]); % Generate random door scenario | |
switch Door_Setup | |
case 1 % Scenario 1 | |
Door = {'Car'; | |
'Goat'; | |
'Goat'}; | |
case 2 % Scenario 2 | |
Door = {'Goat'; | |
'Car'; | |
'Goat'}; | |
case 3 % Scenario 3 | |
Door = {'Goat'; | |
'Goat'; | |
'Car'}; | |
end | |
fprintf('Doors = %s %s %s \n', Door{1}, Door{2}, Door{3}); | |
Door_Chosen = randi([1,3]); % Generate random door number | |
fprintf('Door_Chosen = %i \n', Door_Chosen); | |
while 1; | |
Door_Removed = randi([1,3]); % Choose random door to remove | |
if strcmp(Door(Door_Removed), 'Goat') && Door_Removed ~= Door_Chosen; | |
break % Remove a door with a goat | |
end | |
end | |
fprintf('Door_Removed = %i \n', Door_Removed); | |
switch Always_Switch_Doors % Switch doors if switch is on | |
case 0 % Do not switch doors | |
fprintf('Not Switching Doors \n'); | |
fprintf('Door_Chosen = %i \n', Door_Chosen); | |
case 1 % Switch doors | |
for n = 1:3 | |
if n ~= Door_Chosen && n ~= Door_Removed; | |
Door_Chosen = n; % Final door selected | |
fprintf('Switching Doors \n'); | |
fprintf('Door_Chosen = %i \n', Door_Chosen); | |
break | |
end | |
end | |
end | |
if strcmp(Door(Door_Chosen), 'Car'); % Winner if car door is chosen | |
fprintf('Win \n \n'); | |
Wins = Wins + 1; % Increment win counter | |
else | |
fprintf('Lose \n \n'); | |
end | |
end | |
Losses = Iterations - Wins; % Record simulation statistics | |
Percent = Wins/Iterations*100; % Percentage of games won | |
fprintf(['Always Switch = %1.1i, Wins = %1.1i, Losses = %1.1i,' ... | |
'Percentage = %1.1f \n'], Always_Switch_Doors, Wins, Losses, Percent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment