Skip to content
Snippets Groups Projects
Commit 907c980b authored by Ulrich Kerzel's avatar Ulrich Kerzel
Browse files

update Monty Hall

parent bb8feeb9
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Monty Hall # Monty Hall
In this puzzle, a contestant in a game-show gets to choose between 3 doors. In this puzzle, a contestant in a game-show gets to choose between 3 doors.
Behind one of the doors, a prize (the "car") is hidden, the other two doors do not win (the "goat"). Behind one of the doors, a prize (the "car") is hidden, the other two doors do not win (the "goat").
The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors. The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors.
Is it beneficial to switch the doors? Is it beneficial to switch the doors?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ``` python
import random import random
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ``` python
def monty_hall_simulation(num_trials=5000): def monty_hall_simulation(num_trials=5000):
stay_wins = 0 stay_wins = 0
switch_wins = 0 switch_wins = 0
for _ in range(num_trials): for _ in range(num_trials):
# Randomly place the car behind one of the three doors # Randomly place the car behind one of the three doors
car_door = random.randint(1, 3) car_door = random.randint(1, 3)
# Contestant makes an initial choice randomly # Contestant makes an initial choice randomly
contestant_choice = random.randint(1, 3) contestant_choice = random.randint(1, 3)
# Host opens a door # Host opens a door
possible_doors = [1, 2, 3] possible_doors = [1, 2, 3]
# Host cannot open the door with the car or the contestant's choice # Host cannot open the door with the car or the contestant's choice
doors_host_can_open = [door for door in possible_doors doors_host_can_open = [door for door in possible_doors
if if
door != car_door and door != car_door and
door != contestant_choice] door != contestant_choice]
host_opened_door = random.choice(doors_host_can_open) host_opened_door = random.choice(doors_host_can_open)
# Determine the other door that the contestant can switch to # Determine the other door that the contestant can switch to
remaining_door = [door for door in possible_doors remaining_door = [door for door in possible_doors
if if
door != contestant_choice and door != contestant_choice and
door != host_opened_door][0] door != host_opened_door][0]
# If the contestant stays with their initial choice # If the contestant stays with their initial choice
if contestant_choice == car_door: if contestant_choice == car_door:
stay_wins += 1 stay_wins += 1
# If the contestant switches to the remaining door # If the contestant switches to the remaining door
elif remaining_door == car_door: elif remaining_door == car_door:
switch_wins += 1 switch_wins += 1
stay_win_rate = stay_wins / num_trials stay_win_rate = stay_wins / num_trials
switch_win_rate = switch_wins / num_trials switch_win_rate = switch_wins / num_trials
return stay_win_rate, switch_win_rate return stay_win_rate, switch_win_rate
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ``` python
num_trials = 5000 num_trials = 5000
stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials) stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials)
print(f"After {num_trials} trials:") print(f"After {num_trials} trials:")
print(f"Winning by staying with the initial choice: \t {stay_win_rate*100:.2f}%") print(f"Winning by staying with the initial choice: \t {stay_win_rate*100:.2f}%")
print(f"Winning by switching to the other door: \t {switch_win_rate*100:.2f}%") print(f"Winning by switching to the other door: \t {switch_win_rate*100:.2f}%")
``` ```
%% Output %% Output
After 5000 trials: After 5000 trials:
Winning by staying with the initial choice: 32.60% Winning by staying with the initial choice: 32.60%
Winning by switching to the other door: 67.40% Winning by switching to the other door: 67.40%
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment