# Let's Make a Deal--With Python!

**59**

">

Sometimes when dealing with probabilities, things don't always work out the way you might expect. My favorite example of this is the Monty Hall problem. The name comes from the game show *Let's Make a Deal*, which was hosted by Monty Hall when it debuted in the 1960s. It's still running today (with a different host), and the gameplay remains the same: A contestant chooses among three closed doors--behind one of which is a prize.

Let's say these doors are labeled 1, 2, and 3. The player picks a door--let's go with door number 2. After that, Monty opens one of the other doors to show that it does *not* have the prize. (Suppose it's door number 1.) Now the contestant can either stick with the original choice (door 2) or switch to the other unopened door (door 3). So what would you do? Stick to your guns and stay with door 2, or waffle and go with door 3? (To make this even more stressful, imagine that the audience is yelling their opinions at you--plus you're on television, which always adds some tension.)

It turns out that you are more likely to win the prize if you switch your choice. Yes, I agree-- this doesn't seem to make much sense. I mean, when you first picked a door, you didn't know which one was hiding the prize. And after Monty reveals a losing door, you *still* don't know which one is the winner. Now you are down to two doors instead of three. It doesn't seem like choosing one random door over another would improve your odds--but indeed it does. You start with a 1 out of 3 chance of winning, but if you switch your answer, your chance of winning will now be 2 out of 3.

That seems bizarre, so I want to test this myself. OK, I don't want to *actually* test it. Sure, I could get a friend to make some doors, hide a prize behind one, and let me guess. But unless we do this a whole bunch of times, it will be hard to see if it's really better to switch doors--and nobody has time for this.

How about the next best thing: modeling the problem in Python! It seems like you should be able to solve a problem named after a famous Monty with a programming language named after another famous Monty--in this case, the BBC series *Monty Python's Flying Circu*s.

I'm going to walk you through the basic steps of modeling this with Python. If we can get this to work for one trial, we can just as easily do it for 1,000 or even 10,000 trials.

We need to do two things: model the case without switching doors, and then again using the switching option.

This option is pretty easy to model. We can use a random number generator to pick which door has the prize and then pick another random number for the contestant's choice. If the choice and the prize are the same, it's a win.

For this model, I'm going to use a version of Python called Web VPython that is perfect for physics because it can be used to make 3D objects and move them around. Plus, it runs online so you don't have to install anything. (But I should point out that it handles some things in a different manner than regular Python.)

Our first task is to randomly choose a door for the prize. I'm going to use a random integer between 0 and 2. That's three numbers: 0, 1, 2. (Like many programming languages, Python starts counting from zero.)

After that, I need to randomly pick a number to represent the door the contestant chooses. Again that's 0, 1, or 2. If these two numbers match, the player wins. I can repeat this as many times as I like and count the number of wins divided by the number of tries. That's it. Here's the code.

In the embed below, you can click between the pencil icon to see the code and the arrow icon to view the result.

This is live code, so you can change the number of trials and then run it again. (It's currently set at N = 1,000 in line 4.) If N is quite small, like 10 or 20 trials, then you are going to see some fluctuations in the win percentage. With large numbers, like 1,000, things start to be better behaved. You should be getting a win percentage close to 33, which is about one third, or a one in three chance of winning.

I should add that if the contestant does not switch doors after Monty shows them what's behind one of the losing doors, nothing changes. They already picked their door, so if they stick with it, their odds of winning won't improve. They will remain one in three.

I'll be honest: This option is a bit more complicated to model. Not only do I need to pick the door with the prize and the choice for the contestant, but I need to pick a losing door for Monty to open and then switch the choice of the player.

Before you look at the code below, I want to remind you that I'm a physicist and not a programmer, so it can be considered a little sloppy. Maybe there are better ways to do it, but since I built it, I understand it. It's my code. It's not some black box with a button to push that gives an answer. What I mean is: Your program doesn't have to be perfect. Don't let that stop you from coding.

You can really only see the differences between switching and not switching with larger numbers. (Once again, toggle between the pencil and the arrow to see the code and the results.) This time, if we run 1,000 trials in which the player switches doors after Monty's reveal, they win about two thirds of the time, which is the theoretical solution to this problem.

If you think this seems strange, then I agree with you. Here's the basic explanation: With three doors you have a 1 in 3 probability of winning. When Monty opens one of the doors, he's basically giving you an extra pick--*if you switch*--so you now have 2 in 3 chances of winning.

What if the contestant only plays the game once instead of 1,000 times--will they be able to see that they have improved their chance of winning? Nope. If they only play one time, they will either win or they will lose. They won't really be able to tell if there is a difference between switching doors or not.

What if they played 10 times? In that case, they might win half the games with either method--switching or not switching. (People will also start to wonder how they keep getting on the show.)

Let's imagine that two people separately play the game 100 times each. One person always switches and the other person never does. Both keep track of all the times they win and they make a graph that summarizes the results of both 100-game trials. It would look something like this:

In this simulation, notice that after they play four games, both have the same number of wins. However, after playing 100 games, the door-switching player has won 75 times, but the nonswitching player has only won 35 times. That's very close to the theoretical expectation: two-thirds versus one-third, or 67 percent versus 33. Their scores aren't quite there yet, but they are converging towards that ratio.

This is called the law of large numbers, and it says that as you increase the number of trials that contain random values, the results will approach some expected value. This is an important idea in situations that involve chance, like in casino gambling. For most casino games, the theoretical chance of a player winning is less than 50 percent--so the casino has more than a 50 percent chance of taking their money. If the casino only had a single customer who only played a single game, the house might indeed lose the game, and therefore money. But over the course of a year, with many different people playing many games, the total outcome will approach the expectation value, and the house will win overall.

Notice that for the Monty Hall problem, the expectation value for winning if you switch doors is greater than 50 percent. So in theory, if the players always use the switching strategy, over time the show should lose more games than it wins. Honestly, this is fine. A game show isn't a casino; it can make money even if the players win. But that's not thanks to probability. That's thanks to commercials.