"In this puzzle, a contestant in a game-show gets to choose between 3 doors.\n",
"Behind one of the doors, a prize (the \"car\") is hidden, the other two doors do not win (the \"goat\").\n",
"\n",
"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.\n",
"\n",
"Is it beneficial to switch the doors?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "KuoDvxxe8F3M"
},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yH85YcAI-0eY"
},
"outputs": [],
"source": [
"\n",
"def monty_hall_simulation(num_trials=5000):\n",
" stay_wins = 0\n",
" switch_wins = 0\n",
"\n",
" for _ in range(num_trials):\n",
" # Randomly place the car behind one of the three doors\n",
" car_door = # YOUR CODE HERE\n",
" # Contestant makes an initial choice randomly\n",
" contestant_choice = # YOUR CODE HERE\n",
"\n",
" # Host opens a door\n",
" possible_doors = [1, 2, 3]\n",
" # Host cannot open the door with the car or the contestant's choice\n",
" doors_host_can_open = [# YOUR CODE HERE]\n",
" host_opened_door = # YOUR CODE HERE\n",
"\n",
" # Determine the other door that the contestant can switch to\n",
" remaining_door = [# YOUR CODE HERE][0]\n",
"\n",
" # If the contestant stays with their initial choice\n",
" if contestant_choice == car_door:\n",
" stay_wins += 1\n",
" # If the contestant switches to the remaining door\n",
"print(f\"Winning by staying with the initial choice: \\t {stay_win_rate*100:.2f}%\")\n",
"print(f\"Winning by switching to the other door: \\t {switch_win_rate*100:.2f}%\")"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
%% Cell type:markdown id: tags:
# Monty Hall
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").
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?
%% Cell type:code id: tags:
```
import random
```
%% Cell type:code id: tags:
```
def monty_hall_simulation(num_trials=5000):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
# Randomly place the car behind one of the three doors
car_door = # YOUR CODE HERE
# Contestant makes an initial choice randomly
contestant_choice = # YOUR CODE HERE
# Host opens a door
possible_doors = [1, 2, 3]
# Host cannot open the door with the car or the contestant's choice
doors_host_can_open = [# YOUR CODE HERE]
host_opened_door = # YOUR CODE HERE
# Determine the other door that the contestant can switch to
remaining_door = [# YOUR CODE HERE][0]
# If the contestant stays with their initial choice
if contestant_choice == car_door:
stay_wins += 1
# If the contestant switches to the remaining door
"In this puzzle, a contestant in a game-show gets to choose between 3 doors.\n",
"Behind one of the doors, a prize (the \"car\") is hidden, the other two doors do not win (the \"goat\").\n",
"\n",
"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.\n",
"\n",
"Is it beneficial to switch the doors?"
],
"metadata": {
"id": "aQVYzt-A7so-"
}
},
{
"cell_type": "code",
"source": [
"import random"
],
"metadata": {
"id": "KuoDvxxe8F3M"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "yH85YcAI-0eY"
},
"outputs": [],
"source": [
"\n",
"def monty_hall_simulation(num_trials=5000):\n",
" stay_wins = 0\n",
" switch_wins = 0\n",
"\n",
" for _ in range(num_trials):\n",
" # Randomly place the car behind one of the three doors\n",
" car_door = random.randint(1, 3)\n",
" # Contestant makes an initial choice randomly\n",
" contestant_choice = random.randint(1, 3)\n",
"\n",
" # Host opens a door\n",
" possible_doors = [1, 2, 3]\n",
" # Host cannot open the door with the car or the contestant's choice\n",
" doors_host_can_open = [door for door in possible_doors\n",
"Winning by staying with the initial choice: \t 32.60%\n",
"Winning by switching to the other door: \t 67.40%\n"
]
}
]
}
]
}
\ No newline at end of file
%% Cell type:markdown id: tags:
# Monty Hall
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").
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?
%% Cell type:code id: tags:
```
import random
```
%% Cell type:code id: tags:
```
def monty_hall_simulation(num_trials=5000):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
# Randomly place the car behind one of the three doors
car_door = random.randint(1, 3)
# Contestant makes an initial choice randomly
contestant_choice = random.randint(1, 3)
# Host opens a door
possible_doors = [1, 2, 3]
# Host cannot open the door with the car or the contestant's choice
doors_host_can_open = [door for door in possible_doors