DEV Community

Cover image for Learn how to Simulate the monty hall problem in Python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

Learn how to Simulate the monty hall problem in Python

The original article can be found on kalebujordan.dev

Hello Pythonistas,

Today I will give you a brief guide to applying Python to Mathematics World, on this tutorial you're going to simulate the Monty Hall problem in Python.

You might be wondering what the hell is this Monty Hall Problem?

The Monty Hall problem is a brain teaser, in the form of a probability puzzle, loosely based on the American television game.

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 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?.

Well, It's upon your choice and luck to switch or stay with Door No 1 or switch to number 2, Mathematically the person who switches has twice the chance of winning a Car.

The concept is based on probability concentration. Initially when you choose door No 1. There is a 1/3 probability that the car is in that door and 2/3 in the other two.

But when the host another door on those two remaining and tell there is a goat, then the probability that there is car concentrate to the door No 2.

So Mathematically Switching would be a better idea.

Now we have already learned the concepts, It's time to simulate our idea in Python.

We are going to use the Python random Library to generate random choices and open doors containing goat and a simple tkinter widget to display the overall count.

  • app.py
import random 
from tkinter import StringVar , Label, Tk, Entry

window = Tk()
window.geometry('400x100')
window.title('Monty hall simulation')
window.resizable(0, 0)

same_choice = StringVar()
switched_choice = StringVar()
same_choice.set(0)
switched_choice.set(0)
no_sample = Entry()
Label(text='Same choice').place(x=80,y=8)
Label(text ='Switched choice').place(x=80,y=40)
Label(textvariable = same_choice, font =(15)).pack()
Label(textvariable = switched_choice, font=(15)).pack()
no_sample.pack()


def simulate(event):
    same_choice_result  = 0
    switched_choice_result = 0
    samples = int(no_sample.get())
    doors = ['gold', 'goat', 'bed']
    for _ in range(samples):
        simulated_doors = doors.copy()
        random.shuffle(simulated_doors)
        first_choice = random.choice(simulated_doors)
        simulated_doors.remove(first_choice)
        opened_door = simulated_doors[0] if simulated_doors[0]!='gold' else simulated_doors[1]
        simulated_doors.remove(opened_door)
        switched_second_choice = simulated_doors[0]
        if first_choice == 'gold':
            same_choice_result+=1
            same_choice.set(same_choice_result)
        elif switched_second_choice == 'gold':
            switched_choice_result+=1
            switched_choice.set(switched_choice_result)
        else:
            print('That\'s will never happed')

no_sample.bind('<Return>', simulate)
window.mainloop()

Enter fullscreen mode Exit fullscreen mode

Output

Enter the number of Simulation to be computed by the computer and then press Enter to run the Simulation

As you can see above Switched choice got as twice as chance compared to the one who stayed with the same choice.

Remember choice made in this Simulation is entirely random but still If you gonna try running the Simulation again, you probably gonna get resembling numbers perhaps not exact.

Hope you find this post interesting, don't forget to subscribe to get more tutorials and tips like this. To get the Full code check out on My Github

I also recommend you read these;

In case of any suggestion or comment, drop it in the comment box and I will reply to you immediately.

Top comments (0)