Last active
September 28, 2020 03:20
-
-
Save jsettlem/d088cf9077eec7bf8b61fedb137d5359 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
def simulate_deal_or_no_deal_simple(prizes, swap=False): | |
# setup | |
prizes = prizes.copy() | |
random.shuffle(prizes) | |
case_indices = list(range(len(prizes))) | |
# choose our case | |
my_case = random.choice(case_indices) | |
case_indices.remove(my_case) | |
# open all the other cases | |
while len(case_indices) > 1: | |
random_case = random.choice(case_indices) | |
case_indices.remove(random_case) | |
# swap? | |
if swap: | |
other_case = case_indices[0] | |
my_case = other_case | |
# result | |
return prizes[my_case] | |
def simulate_deal_or_no_deal_with_short_circuit(prizes, swap=False): | |
min_prize, max_prize = min(prizes), max(prizes) | |
# setup | |
prizes = prizes.copy() | |
random.shuffle(prizes) | |
case_indices = list(range(len(prizes))) | |
# choose our case | |
my_case = random.choice(case_indices) | |
# short circuit | |
if prizes[my_case] != min_prize and prizes[my_case] != max_prize: | |
return False | |
case_indices.remove(my_case) | |
# open all the other cases | |
while len(case_indices) > 1: | |
random_case = random.choice(case_indices) | |
case_indices.remove(random_case) | |
other_case = case_indices[0] | |
# short circuit | |
if prizes[other_case] != min_prize and prizes[other_case] != max_prize: | |
return False | |
# swap? | |
if swap: | |
my_case = other_case | |
# result | |
return prizes[my_case] | |
if __name__ == '__main__': | |
iterations = 1_000_000 | |
prizes = [0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, | |
1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000] | |
# prizes = [0.01] * 25 + [1_000_000] | |
total_prize = 0 | |
runs = 0 | |
for i in range(iterations): | |
outcome = simulate_deal_or_no_deal_with_short_circuit(prizes=prizes, swap=False) | |
if outcome: | |
runs += 1 | |
total_prize += outcome | |
print(f"Average prize if you don't swap ({runs} runs): ${total_prize / runs}") | |
total_prize = 0 | |
runs = 0 | |
for i in range(iterations): | |
outcome = simulate_deal_or_no_deal_with_short_circuit(prizes=prizes, swap=True) | |
if outcome: | |
runs += 1 | |
total_prize += outcome | |
print(f"Average prize if you do swap ({runs} runs): ${total_prize / runs}") |
This file contains hidden or 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 | |
def simulate_monty(door_count=3, swap=False) -> bool: | |
# setup | |
doors = ["π"] * (door_count - 1) + ["π"] | |
random.shuffle(doors) | |
# our turn | |
my_door = random.randrange(0, door_count) | |
# Monty's turn | |
if doors[my_door] == "π": | |
montys_door = doors.index("π") | |
else: | |
montys_door = random.choice( | |
[door for door in range(len(doors)) if door != my_door] | |
) | |
# our turn | |
if swap: | |
my_door = montys_door | |
# result | |
return doors[my_door] == "π" | |
if __name__ == '__main__': | |
runs = 1_000_000 | |
doors = 26 | |
wins = 0 | |
for _ in range(runs): | |
wins += simulate_monty(door_count=doors, swap=False) | |
print(f"We won {wins} times out of {runs} without swapping.") | |
wins = 0 | |
for _ in range(runs): | |
wins += simulate_monty(door_count=doors, swap=True) | |
print(f"We won {wins} times out of {runs} while swapping") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment