Skip to content

Instantly share code, notes, and snippets.

@jswetzen
Created April 9, 2016 10:37
Show Gist options
  • Save jswetzen/8b05334045145355d53d995cb69ced2b to your computer and use it in GitHub Desktop.
Save jswetzen/8b05334045145355d53d995cb69ced2b to your computer and use it in GitHub Desktop.
Simulate the classical game show problem of choosing one of three doors. One of the wrong doors are exposed, so what do you do? Stay, switch or randomly choose which to do? Try this a million times to see what the statistics say.
#!/usr/bin/env python3
import random
def remove_door(doors, win, pick1):
doors.remove(win)
try:
doors.remove(pick1)
doors = [win, pick1]
except ValueError:
del doors[random.choice((0, -1))]
doors.append(win)
return doors
def test_prob() :
doors = ['l', 'm', 'r']
win = random.choice(doors)
pick1 = random.choice(doors)
doors = remove_door(doors, win, pick1)
switch = [x for x in doors if x != pick1][0]
pick2 = random.choice(doors)
return (win == pick1, win == switch, win == pick2)
tries = 1000000
wins = [0, 0, 0]
for y in range(tries):
result = test_prob()
for i in range(3):
if result[i]:
wins[i] += 1
wins = [x/tries*100 for x in wins]
print("stay: {:.2f}%\nswitch: {:.2f}%\nrandom again: {:.2f}%".format(
wins[0], wins[1], wins[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment