Last active
October 23, 2015 17:05
-
-
Save ndunn219/5788286c294744c748a5 to your computer and use it in GitHub Desktop.
This is a Python model for the Monty Hall problem. See comment below for credit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class MontyHall: | |
def __init__(self): | |
self._prize_door = random.randint(1,3) | |
self._selected_door = random.randint(1,3) | |
self._removed_door = self.remove_door() | |
def remove_door(self): | |
doors = [1,2,3] | |
doors.remove(self._prize_door) | |
if self._selected_door in doors: | |
doors.remove(self._selected_door) | |
return random.choice(doors) | |
def switch_choice(self): | |
self._selected_door = 6 - self._selected_door - self._removed_door | |
def run(self, switch=True): | |
if switch: | |
self.switch_choice() | |
return self._selected_door == self._prize_door | |
wins = 0 | |
n = 1000 | |
switch = False | |
for i in range(n): | |
m = MontyHall() | |
if m.run(switch=switch): | |
wins += 1 | |
odds = (wins / n) * 100 | |
print('''- Switching: {} | |
- Simulations: {:,} | |
- Wins: {} | |
- Winning Percentage: {:.1f}%'''.format(switch, n, wins, odds)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a slightly modified version of Jason Hill's Monty Hall Problem model, of which I made a YouTube video.