In Projects

MATLAB Three Doors Game Simulation

There is a game show where a contestant attempts to pick the correct door amongst three doors. Two of the doors hide goats behind them and one of the doors hides a new car behind it. The player attempts to pick the door with the car behind it to win the new car as a prize. Once the contestant chooses a door, the game host then opens one of the other doors to reveal one of the goats. The host then asks the contestant if they would like to stick with the door they chose or if they would like to change their mind and pick the remaining closed door. What should the contestant do?

The following cases will always prove true:

1) If the contestant does not switch doors, then they must origianlly pick the correct door in order to win (33.3% chance of winning)

2) If the contestant does switch doors, then they must originally pick one of the goat doors in order to win (66.7% chance of winning)

If the contestant does not switch doors then his odds of winning are about 33%. If the contestant switches doors then they increases their odds of winning to about 66%. The advantage of switching doors may not be obvious at first. The MATLAB simulation here demonstrates this statistical advantage by simulating 1000 rounds of the game.

MATLAB Code Download

Download the MATLAB program.

        % Program:
        % Three_Doors_Game_Show_Simulation.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';
                case 2                          % Scenario 2
                    Door = {'Goat'; 
                case 3                          % Scenario 3
                    Door = {'Goat';
            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
            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);
            if strcmp(Door(Door_Chosen), 'Car');    % Winner if car door is chosen
                fprintf('Win \n \n');
                Wins = Wins + 1;                    % Increment win counter
                fprintf('Lose \n \n');
        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);