function proportion = MontyHall(num_doors,switch_choice,num_simulations)
% MontyHall should return the probabiliyt of winning if you're on the
% game show with num_doors doors and you pick one door -- Monty then shows
% you all but one of the remaining doors. switch_choice should be 0 or 1 --
% if switch_choice is 0, you don't switch, and if it is 1, you do. 
% Run this simulation num_simulations times and output the proportion of
% times that you win. 

results = zeros(1,num_simulations); % this will hold our results for each 
                                    % simulation, so 1 for a win and 0
                                    % otherwise
                                    
for i = 1:num_simulations
    
    winning_door = randi(num_doors); % choose winnning dooor
    your_choice = randi(num_doors);  % choose your door
    
    if your_choice == winning_door
        door_monty_doesnt_show = randi(num_doors); 
        % If you picked the right door to begin with, Monty must leave open
        % another door at random (so he opens all but one).        
        while(door_monty_doesnt_show == your_choice)  
            % Here, we make sure that Monty hasn't picked your door. If he 
            % did, we make him choose again until he's chosen a different 
            % door.
            door_monty_doesnt_show = randi(num_doors);
        end
    else
        door_monty_doesnt_show = winning_door; 
        % If you didn't pick the right door to begin with, Monty must leave
        % the winning door open.
    end

    if switch_choice == 1
        your_choice = door_monty_doesnt_show; % you've switched
    end

    if your_choice == winning_door %if you win, 1, if not, 0.
        result = 1;
    else 
        result = 0;
    end
    
    results(i) = result; % record results
end

proportion = sum(results)/num_simulations; % take proportion.
end