Monty Hall Problem Code to Proof

Started by
25 comments, last by irreversible 7 years, 1 month ago

Theres something wrong here..

https://en.wikipedia.org/wiki/Monty_Hall_problem

First...Thats gotta be bullshit, 50% chances are always 50% chances, u dont dare stack chances man u_u**, what dimension is this?? I dont get it..

So I went and coded it, but its not even just getting me better chances, its getting me ~60% wins if I change the door, and ~30% wins if I keep the door? Then I went and added a pick one of two chances with the same code structure to check if Im not doing some retarded stuff, but then I do get an average of ~50% wins..

It gotta be something related to random numbers not being true random or something...Can someone spot issues? I need issues to keep going with my life, that doesnt make any sense


using System;

public class Test
{
    enum prize{ goat, car};

	Random rnd = new Random();//Environment.TickCount);
	Random rndPick = new Random(Environment.TickCount); // just trying to see if makes a difference
    
    void Shuffle(ref prize[] array){
        
        //Console.WriteLine("shuffling");
		int n = array.Length;
		prize[] doors = new prize[n];

		for (int it = 0, nElementsRemaining = n; it < n; ++it)
		{ 
           // Console.WriteLine("it = "+it);
            
            int randomIndex = rnd.Next(0, nElementsRemaining);
            //Console.WriteLine("randomIndex = "+randomIndex);
            doors[it] = array[randomIndex];
            
            array[randomIndex] = array[--nElementsRemaining];
             //Console.WriteLine("nElementsRemaining = "+nElementsRemaining);
             
            // string szarray =
            //array[0].ToString() + " " + array[1].ToString() + " " + array[2].ToString();
            //Console.WriteLine("a = "+szarray);
        }
        
        array = doors;
        //Console.WriteLine("--------");
    }


	public void Execute(bool changeDoor, int tries){

		int currTries = tries;//5000000;
        int wins = 0;
        int loses = 0;

		while (--currTries >= 0)
		{ 
        
           // Console.WriteLine("==================================");
        
            prize[] doors = new prize[]{prize.goat, prize.goat, prize.car};
            Shuffle(ref doors);
            
           // string szDoors =
           // doors[0].ToString() + " " + doors[1].ToString() + " " + doors[2].ToString();
            //Console.WriteLine("szDoors = "+szDoors);



			int pickDoor = rndPick.Next(0, 3);
           // Console.WriteLine("pickDoor = "+pickDoor + "\n=============================");
            int eliminatedDoor = 0;
            while(true){
                
                if( eliminatedDoor  != pickDoor && doors[eliminatedDoor ] == prize.goat ) break;
                ++eliminatedDoor; 
            }
           // Console.WriteLine("eliminatedDoor = "+eliminatedDoor);
			if (changeDoor == true)
			{
            
                bool[] doorsRemaining = new bool[] {true, true, true};
                doorsRemaining[pickDoor] = false;
                doorsRemaining[eliminatedDoor] = false;
                
                pickDoor = 0;
                while(true){
                    
                    if( doorsRemaining[pickDoor] == true ) break;
                    pickDoor++;
                }
            }
            
            if( doors[pickDoor] == prize.car ) wins++;
            else loses++;
	    }
	    
	    Console.WriteLine("wins:  " + wins + "\n" + "loses: "+ loses);
		float avgWins = (float)wins / (float)tries;
		Console.WriteLine("avg:  " + avgWins.ToString("0.0###"));
	}

	public void ExecutePicOfTwo(int tries)
	{
		int currTries = tries;//5000000;
        int wins = 0;
		int loses = 0;

		while (--currTries >= 0)
		{
			prize[] doors = new prize[] { prize.goat, prize.car };
			Shuffle(ref doors);

			int pickDoor = rndPick.Next(0, 2);

			if (doors[pickDoor] == prize.car) wins++;
			else loses++;	
		}

		Console.WriteLine("wins:  " + wins + "\n" + "loses: " + loses);
		float avgWins = (float)wins / (float)tries;
		Console.WriteLine("avg:  " + avgWins.ToString("0.0###"));
	}

	public static void Main()
	{
		Console.WriteLine("Press enter to step.");
		while (true)
		{
			int tries = 50000;

			Console.ReadLine();
			Test newTest = new Test();
			Console.WriteLine("pic of two:");
			newTest.ExecutePicOfTwo(tries);

			Console.ReadLine();
			//Test newTest = new Test();
			Console.WriteLine("keep door:");
			newTest.Execute(false, tries);

			Console.ReadLine();
			Console.WriteLine("change door:");
			newTest.Execute(true, tries);
		}
	}
		
}
Advertisement

Its actually just pure statistics (as far as I understood).

You pick a door. Chances are 33% to pick the correct door, 66% to pick the incorrect door.

Now heres the twist: If you pick an incorrect door (66% of the time), the host hast to show you the other incorrect door, leaving the last door as the correct one (since you occupy an incorrect door, and one of the other doors has the car, so he does not have a choice of which door to open in that 66% case).

So here's the possible outcomes:

33%, picked right door and stays => wins.

66%, picked wrong door and stays => looses.

66%, picked wrong door and switches => wins.

33%, picked right door and switches => looses.

(see how the chances on winning/loosing are inverse for staying/switching)

So thus, switching should give you a better outcome alone on the basis that its much more likely to pick a goat-door, and switching in this case guarantees to pick the right door. If you somehow picked the car-door the first time, too bad, but its far less likely. So the numbers you see should be correct.

The crux lies in the fact that the door that is opened (by the gameshow presenter or similar) is not chosen at random. The opened door has to be a "bad" door. Thus it is not chosen at completely random, as it is influenced by the initial random pick of a door.

Also, when approached with a math/statistics puzzle complete with proof, calling it bullshit because you don't intuitively agree with it is irrational at best.

Hello to all my stalkers.

I usually find it's more intuitive when you scale up the number of doors:

Imagine 100 doors, 99 of them are duds and 1 of them wins the game.

The gameshow host asks you to pick a door. Obviously you have a 1/100 chance of choosing the winning door and you have no option but to choose a door at random.

This leaves 99 doors and the gameshow host proceeds to open all other doors to reveal duds except he keeps 1 of these 99 doors closed.

So there are 2 closed doors to choose from now: The one you initially picked and the one left alone by the gameshow host.

We know that your door was selected at random out of 100 while the other door was suspiciously left closed by the gameshow host and out of all the 99 doors he decided to keep that particular one closed!

Now you have a choice - do you stick with your first choice or swap to the other door?

There are only 2 doors to choose from, but does it really feel like a 50/50 chance to you now?

You know that one cassino example:

You keeping betting low values on a color. Till you get some high sequence of the same colors.

black, red, black, black, black, black

Now, chances are the next one will be red, so you bet everything...and ure screwed cause 50% chances is always 50% chances..

Statistically speaking, that means that using this technique you could increase ur money (cause for a fixed number of tries, u know the average number that each color will be pick)..but does this work in practice like the Monty Hail case? No..I guess...I dunno anymore..I mean, if this works everyone would do it..but I dunno how cassinos work.

I though this one was the same case.

But them every time I run the code, its always the same %, not a single time I manage to get lower then 50% wins, even with 10 tries you have super high win score, with 50 tries its already dead win

I need issues to keep going with my life, that doesnt make any sense
Sorry for your life, but our intuition and expectations with chances is just broken.

I learned a long time ago to trust a computed stochastic result more than my own guts, so for anything with chances, I convert to a stochastic problem, and do the computations (or a simulation like you do). That's the real answer, no matter what my instinct tells me.

The missing thing that explains the stochastic result often comes much later for me. I figured this one out as well, and in this case it is as Lactose said. The show host brings in new information, which changes the odds. If you stay with your old choice, you're not using that new information. If you switch, you do, and the chances shift in your favor.

You know that one cassino example:

You keeping betting low values on a color. Till you get some high sequence of the same colors.
black, red, black, black, black, black
Now, chances are the next one will be red, so you bet everything...and ure screwed cause 50% chances is always 50% chances..
Statistically speaking, that means that using this technique you could increase ur money (cause for a fixed number of tries, u know the average number that each color will be pick)..but does this work in practice like the Monty Hail case? No..I guess...I dunno anymore..I mean, if this works everyone would do it..but I dunno how cassinos work.


If you're talking about roulette, players with "infinite money" still statistically lose in the long run due to the 0 and 00 "house numbers" on the wheel. The payouts on all other options do not factor in those slots, but the odds of landing on a particular slot still do, so the payout/odds ratios are always less than break-even for the player.

https://en.wikipedia.org/wiki/Roulette#Bet_odds_table
Here's another fun probability problem: https://en.wikipedia.org/wiki/Bertrand's_box_paradox
My mom always said I was a bit of a Martian (hence my choice of avatar), so perhaps that's why I don't seem to have any problems with any of these questions.

A slightly more confusing probability problem is this one: http://news.bbc.co.uk/2/hi/programmes/more_or_less/8735812.stm
Now, chances are the next one will be red, so you bet everything...and ure screwed cause 50% chances is always 50% chances..

Yes, and how does that compare to the monty hall problem? Montey Hall isn't 50/50. I mean its pretty obvious that you didn't properly read the article nor watched the video (which explains that its exactly 33/66 as I've watched it to confirm I'm correct). But at least now with the answers you should be able to understand that, which you don't seem to do. Jep, 50/50 is always 50/50, but when its not 50/50 it is not 50/50... like rolling a dice, you cannot say "well its always 50/50 if I roll a 1 or not", which is essentially what this boils down to when applied to the monty hall problem (only that this problem is a tad bit more confusing than a dice, to be fair) :D

This topic is closed to new replies.

Advertisement