Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Article

The Monty Hall Problem - C# Solution

Rate me:
Please Sign up or sign in to vote.
3.56/5 (9 votes)
6 Nov 2006CPOL3 min read 95.4K   328   10   32
C# Solution to the "Monty Hall Problem"

Introduction

In a recent episode of Numb3rs, the television drama sometimes involving the theoretical use of mathematics to solve real world crime problems "Charlie" the Mathematician gave a public seminar demonstrating some math's puzzles. One of those demonstrated was the "Monty Hall ParadoxWikipedia describes this puzzle as:

"The Monty Hall problem is a puzzle involving probability loosely based on the American game show Let's Make a Deal. The name comes from the show's host, Monty Hall. A widely known, but problematic (see below) statement of the problem is from Craig F. Whitaker of Columbia, Maryland in a letter to Marilyn vos Savant's September 9, 1990, column in Parade Magazine (as quoted by Bohl, Liberatore, and Nydick).

Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?"

The question in a nutshell is, after the host removes one of the wrong doors, is there any advantage to changing your answer. Is it now a 50/50 chance? Or does staying or changing your selection increase OR decrease your odds of winning?

The Experiment

I wrote a simple console application that run 1 million iterations of The Monty Hall gameshow segment. The proof carries out the following process:

  1. Randomly choose 1 of 3 doors for the initial contestant's pick
  2. Randomly pick one of the wrong doors to remove (the host knows which one has the car, and removes an INCORRECT choice)
  3. Change the contestant's picked door
  4. Determine if the contestant WON or LOST!

Did the contestant win after switching?

Result when SWITCHING your choice after the host has removed one incorrect answer:

Wins: 666576 Losses: 333424 Total: 1000000

If you initially think that there is no advantage in switching doors, or your probability is 50/50, or your probability remains at 1 out of 3 - you are in great company, but still INCORRECT.

Switching your initial choice after the host has disclosed one incorrect door DOUBLES your chances of winning to 2 out of 3.

I must admit, until I wrote this proof I wasn't exactly confident. It clearly demonstrates that approximate 66% of the time you would WIN if you SWITCHED your answer, and only win 33% of the time if you kept your initial choice.

I was also amazed at the almost perfect ratio 66.6 / 33.3 which matched the theoretical 2 out of 3 win/loss prediction. Kudos to the authors of the built-in .NET Random class. I didn't initially believe the results because the numbers worked out so nice, but I've double-checked my code and made certain that I've used random decisions for all moving parts. I'd be interested if anyone can point-out anything wrong with my code though.

The Code

C#
using System;

namespace MontyHall
{
    class Program
    {
        static void Main(string[] args)
        {
            // local variables to hold result and random generator
            Random random = new Random();
            int wins   = 0;
            int losses = 0;

            // iterate our MontyHall routine
            for (int i = 0; i < 1000000; i++)
            {
                // changeDoor: 
                // 0 = no, the contestant stays with their initial pick,
                // after the offer to switch after
                // the disclosure of a "Goat" door
                // 1 = yes, the contestant chose to switch doors after
                // the disclosure of a "Goat" door
                //int changeDoor = 0;
                int changeDoor = 1;
                
                // calculate whether or not the contestant wins the Car -
                // random pickedDoor: 0, 1 or 2 for the door
                // the contestant initially picked
                // changeDoor: 0 = no, 1 = yes. The contentment decides
                // to change their selection after disclosure of a "Goat" door
                // random carDoor: 0, 1 or 2 for the door containing the car
                // random goatDoorToRemove: 0 = leftmost Goat door,
                // 1 = rightmost Goat door. Monty discloses
                // one incorrect door, this value indicates which one.
                bool result = MontyHallPick(random.Next(3), changeDoor, 
                                            random.Next(3), random.Next(1));

                if (result) 
                    wins++;
                else
                    losses++;
            }

            Console.WriteLine("Wins: {0} Losses: {1}  Total: {2}", 
                              wins, losses, wins+losses);
            Console.ReadLine();        
        }

        public static bool MontyHallPick(int pickedDoor, int changeDoor, 
                                      int carDoor, int goatDoorToRemove)
        {
            bool win = false;

            // randomly remove one of the *goat* doors,
            // but not the "contestants picked" ONE!
            int leftGoat  = 0;
            int rightGoat = 2;
            switch (pickedDoor)
            {
                case 0: leftGoat = 1; rightGoat = 2; break;
                case 1: leftGoat = 0; rightGoat = 2; break;
                case 2: leftGoat = 0; rightGoat = 1; break;
            }

            int keepGoat = goatDoorToRemove == 0 ? rightGoat : leftGoat;

            // would the contestant win with the switch or the stay?
            if (changeDoor == 0)
            {
                // not changing the initially picked door
                win = carDoor == pickedDoor;
            }
            else
            {
                // changing picked door to the other door remaining
                win = carDoor != keepGoat;
            }

            return win;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Troy Magennis is a C# MVP, born and raised in Sydney, Australia but now living in Seattle, WA in the United States. Troy spends whatever time he doesn't spend writing or talking about writing C# .NET code in the outdoors, hiking or skiing. Current hobby projects include writing about the upcoming C# features, writing a suit of unit tests for the Standard Query Operators for LINQ, and a set of additional extension methods all available from his Blog: http://blog.aspiring-technology.com

Comments and Discussions

 
Questionrandom.Next(1) always return 0 Pin
BMicka19-Nov-21 4:18
BMicka19-Nov-21 4:18 
QuestionSurprise Pin
BMicka19-Nov-21 0:06
BMicka19-Nov-21 0:06 
QuestionWhat if... Pin
Warrick Procter13-Jan-08 18:16
Warrick Procter13-Jan-08 18:16 
The maths is rather simple, the statistical testing fine, I understand it well.

But what if you actually wanted a goat? Poke tongue | ;-P

Troft not lest ye be sponned on the nurg! (Milligan)

QuestionCannot how to work Solution Explorer Pin
Member 38867704-Mar-07 21:43
Member 38867704-Mar-07 21:43 
QuestionCannot how to work Solution Explorer Pin
Member 38867704-Mar-07 21:41
Member 38867704-Mar-07 21:41 
QuestionCannot how to work Solution Explorer Pin
Member 38867704-Mar-07 21:29
Member 38867704-Mar-07 21:29 
GeneralAn intuitive comment ... a more formal approach Pin
Maurice Tarrant14-Nov-06 19:12
Maurice Tarrant14-Nov-06 19:12 
GeneralRe: An intuitive comment ... a more formal approach Pin
Greg Mulvihill5-Feb-07 15:25
Greg Mulvihill5-Feb-07 15:25 
GeneralAn intuitive comment ... Pin
Maurice Tarrant14-Nov-06 16:29
Maurice Tarrant14-Nov-06 16:29 
GeneralLight dawns! Pin
Dave Cross13-Nov-06 23:27
professionalDave Cross13-Nov-06 23:27 
JokeCongratulations ... Pin
Ilíon7-Nov-06 4:10
Ilíon7-Nov-06 4:10 
GeneralWell, while you're at it Pin
Cliff Stanford1-Nov-06 6:43
Cliff Stanford1-Nov-06 6:43 
GeneralFunction flaw [modified] Pin
fera1-Nov-06 3:45
fera1-Nov-06 3:45 
GeneralRe: Function flaw Pin
Troy Magennis1-Nov-06 7:57
Troy Magennis1-Nov-06 7:57 
GeneralRe: Function flaw Pin
fera2-Nov-06 9:39
fera2-Nov-06 9:39 
GeneralNot a proof Pin
juggler1-Nov-06 2:53
juggler1-Nov-06 2:53 
GeneralRe: Not a proof Pin
Troy Magennis1-Nov-06 7:58
Troy Magennis1-Nov-06 7:58 
QuestionWhy? Pin
Paul M Watt31-Oct-06 19:23
mentorPaul M Watt31-Oct-06 19:23 
AnswerRe: Why? Pin
GaryWoodfine 31-Oct-06 22:30
professionalGaryWoodfine 31-Oct-06 22:30 
AnswerRe: Why? Pin
joelgarabedian31-Oct-06 23:46
joelgarabedian31-Oct-06 23:46 
AnswerRe: Why? Pin
RexNebular1-Nov-06 1:15
RexNebular1-Nov-06 1:15 
GeneralRe: Why? Pin
homerbush1-Nov-06 5:27
homerbush1-Nov-06 5:27 
GeneralRe: Why? Pin
DancnDude13-Nov-06 9:56
DancnDude13-Nov-06 9:56 
AnswerRe: Why? Pin
James Curran1-Nov-06 5:24
James Curran1-Nov-06 5:24 
GeneralRe: Why? Pin
Steven Roebert1-Nov-06 8:32
Steven Roebert1-Nov-06 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.