# My Monty Hall PHP Script

Well, actually, it’s not mine. It was a script in Linux Format Magazine a few issues ago. As hobbyists have done since the beginning, I copied the code down to see it actually work.

First the background on Monty Hall. Here it is from Wikipedia.com:

The Monty Hall problem is a puzzle in game theory involving probability that is loosely based on the American game show Let’s Make a Deal. The name comes from the show’s host, Monty Hall. In this puzzle a player is shown three closed doors; behind one is a car, and behind each of the other two is a goat. The player is allowed to open one door, and will win whatever is behind the door. However, after the player selects a door but before opening it, the game host (who knows what’s behind the doors) must open another door, revealing a goat. The host then must offer the player an option to switch to the other closed door. Does switching improve the player’s chance of winning the car? The answer is yes — switching results in the chances of winning the car improving from 1/3 to 2/3.

Here is a famous statement of the problem, from a letter from Craig F. Whitaker to Marilyn vos Savant’s column in Parade Magazine in 1990 (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?

This is a restatement of the problem as given by Steve Selvin in a letter to the American Statistician (February, 1975). As stated, the problem is an extrapolation from the game show; Monty Hall did open a wrong door to build excitement, but did not allow players to change their choice. As Monty Hall wrote to Selvin:

And if you ever get on my show, the rules hold fast for you—no trading boxes after the selection.

Selvin’s subsequent letter to the American Statistician (August, 1975) appears to be the first use of the term “Monty Hall problem”.

An essentially identical problem appeared as the “three prisoners problem” in Martin Gardner’s Mathematical Games column in 1959. Gardner’s version makes the selection procedure explicit, avoiding the unstated assumptions in the Parade Magazine version.

The first appearance of the problem was probably the one presented in Joseph Bertrand’s Calcul des probabilités (1889) where it was known as Bertrand’s Box Paradox.

An unambiguous statement of the problem, with explicit constraints on the host as described by Mueser and Granberg:

* Behind each of three doors is either a goat or a car (two goats, one car), with the car behind each door with equal probability.
* The player picks one of the three doors. The contents are not revealed.
* The game host knows what is behind each door.
* The game host must open one of the remaining doors and must make the offer to switch.
* The game host will always open a door with a goat.
o That is, if the player picks a door with a goat, the game host picks the other door with a goat.
o And if the player picks the door with a car, the game host randomly picks either of the two doors with a goat.
* The host offers the player the chance to either claim what is behind the originally-chosen door, or to switch, claiming what is behind the one remaining door.

Do the player’s odds of getting the car increase by switching?

The solution

The answer to the problem is yes; the chance of winning the car is doubled when the player switches to another door rather than sticking with the original choice.

There are three possible scenarios, each with equal probability (1/3):

* The player picks goat number 1. The game host picks the other goat. Switching will win the car.
* The player picks goat number 2. The game host picks the other goat. Switching will win the car.
* The player picks the car. The game host picks either of the two goats. Switching will lose.

In the first two scenarios, the player wins by switching. The third scenario is the only one where the player wins by staying. Since two out of three scenarios win by switching, the odds of winning by switching are 2/3.

The problem would be different if there were no initial choice, or if the game host picked a door to open at random, or if the game host were permitted to make the offer to switch more often (or only) depending on knowledge of the player’s original choice. Some statements of the problem, notably the one in Parade Magazine, do not explicitly exclude these possibilities. For example, if the game host only offers the opportunity to switch if the contestant originally chooses the car, the odds of winning by switching are 0%. In the problem as stated above, it is because the host must make the offer to switch and must reveal a goat that the player has a 2/3 chance of winning by switching.

Another way of getting the solution is that assuming you will switch, the only way of losing would be by originally picking the winning door (i.e. you inititially bet that you’ll find the prize; if you did pick the winning door, switching will make you lose). By switching, you essentially invert your chances from 1/3 to 2/3 (i.e. by switching you actually bet on not having chosen the winning door in the first pick).

Don’t believe it? Try my Monty Hall Simulation. The more rounds you play the closer the results follow the math. Basically, it’s like how you have a 50/50 chance of getting a heads when flipping a coin. In real life you could flip 10 heads in a row. But if you did 100, it’d be closer to 50/50.

Try it out!