Skip to content

Instantly share code, notes, and snippets.

@TheTexasDev
Created March 11, 2024 18:41
Show Gist options
  • Save TheTexasDev/9180663bf3aac18f5d1fce935384e738 to your computer and use it in GitHub Desktop.
Save TheTexasDev/9180663bf3aac18f5d1fce935384e738 to your computer and use it in GitHub Desktop.
Proof of the Monte Hall problem which I personally disagree with. Picks a random door and decides whether to switch or stay. Logs the wins and percentages at the end
from random import randint
switching_wins = 1
staying_wins = 1
switching_count = 1
staying_count = 1
total_runs = 0
stop_at = randint(10000,100000) # Set to how many instances you wanna try. Bigger the sample size the less room for error
when_to_swap = round(stop_at/2) # The point where to stop switching from the initally picked door. By default it will equally switch and stay. Can be set to anything less than stop_at
switch_randomly = False # If set to true, instead of switching at a set number the program will randomly decide to switch or not every time
log_every_attempt = False
out_message = "Picked %s; Prize: %s; Removed %s; Switched: %r; %s; Switching Win percent: %r - Staying Win percent: %r" #Picked (first pick); Removed (Door without prize); Switched: (True/False); (Win/Loss)
print("Opening doors...\n")
while total_runs < stop_at:
doors = ["A","B","C"] # The list of doors
no_prize = ["A","B","C"] # This is edited down 2 lines to be just the doors without the prize
prize_index = randint(0,2)
prize_door = no_prize.pop(prize_index) # The door with the prize behind it and elimantes the prize door from no_prize
first_pick = randint(0,2)
if first_pick == prize_index:
remove = no_prize.__getitem__(randint(0,1))
else:
remove = 2
# Will remove whichever door is not picked and is not the prize
while remove == first_pick or remove == prize_index:
remove -= 1
remove = doors.__getitem__(remove)
if switch_randomly:
do_i_switch = [True,False].__getitem__(randint(0,1))
elif total_runs >= when_to_swap:
do_i_switch = False
else:
do_i_switch = True
if do_i_switch:
second_pick = 0
# Same as the loop above. But instead of avoiding the first pick and prize, it avoids the first pick and removed door
while second_pick == first_pick or doors.__getitem__(second_pick) == remove:
second_pick += 1
else:
second_pick = first_pick
final_verdict = doors.__getitem__(second_pick)
winloss = "Win"
if do_i_switch:
switching_count += 1
if second_pick == prize_index:
switching_wins += 1
else:
winloss = "loss"
else:
staying_count += 1
if second_pick == prize_index:
staying_wins += 1
else:
winloss = "loss"
switch_win_percent = round((switching_wins/switching_count)*100,2)
stayed_win_percent = round((staying_wins/staying_count)*100,2)
if log_every_attempt:
print(out_message%(doors.__getitem__(first_pick),prize_door,remove,do_i_switch,winloss,switch_win_percent,stayed_win_percent))
total_runs += 1
print("\n")
print("Ran %r Times. Switched %r and Stayed %r times.\nSwitched Wins: %r / Switch Win Rate: %r\nStayed Wins: %r / Stay Win Rate: %r"%(total_runs,switching_count,staying_count,switching_wins,switch_win_percent,staying_wins,stayed_win_percent))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment