Skip to content

Instantly share code, notes, and snippets.

@rahimnathwani
Created October 8, 2020 07:31
Show Gist options
  • Save rahimnathwani/2b6ca328a74b37b952c75d14b6b34a93 to your computer and use it in GitHub Desktop.
Save rahimnathwani/2b6ca328a74b37b952c75d14b6b34a93 to your computer and use it in GitHub Desktop.
import random
donkey = 0
car = 10 # car is worth more than a donkey
prizes = [car, donkey, donkey] # there's always exactly one car
N = 1000000
def pick_a_door(prizes):
random.shuffle(prizes)
my_pick, *other_doors = prizes
return [my_pick, other_doors]
def keep_or_swap(my_picks, keep=False):
if keep:
return my_picks[0]
else:
# if you switch, and one of the other doors had the car, you
# will definitely get it, so you get the max() of those two doors
return max(my_picks[1])
print(sum([keep_or_swap(pick_a_door(prizes)) for i in range(N)]) / N)
print(sum([keep_or_swap(pick_a_door(prizes), keep=True) for i in range(N)]) / N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment