Monty Hall Problem Revisited

Ok, so I found this site called lazyweb.org that invites other users to solve my problems (not in the ‘how do i land a girlfriend? / why can’t i stop eating toblerone?’ sense, but realistic ones that have an answer). My problem was my Monty Hall program results were a bit err.. wrong. So I submitted the Monty Hall Problem and a very nice man called Thomas David Baker from London sent me the code for his Monty Hall program that actually produces the results I was looking for. So the book is right and Andy isn’t and I need to become a better programmer.

Here is Thomas’ C# code –

using System;
using System.Collections;

namespace ConsoleApplication1
{
    /// <summary>
    /// Showing the stats for the Monty Hall problem.
    /// </summary>
    class MontyHall
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            int games = (args.Length > 0 ? Convert.ToInt32(args[0]) : 10000);
            Random r = new Random();

            int correctWhenStick = 0;
            int correctWhenChange = 0;

            for (int i = 0; i < games; i++)
            {
                int rightAnswer = r.Next(3);
                int initialGuess = r.Next(3);

                if (initialGuess != rightAnswer)
                {
                    // your initial guess is wrong
                    // the other booby prize door is open
                    // so to change is to get the prize.
                    correctWhenChange++;
                }
                else if (initialGuess == rightAnswer)
                {
                    // you got the prize right first time.
                    // so to stick is to get the prize.
                    correctWhenStick++;
                }
            }
            Console.WriteLine("Over " + games +
                " games, changing wins " + correctWhenChange +
               " times  and sticking wins " + correctWhenStick + " times.");
        }
    }
}
Gary

Gary