Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created December 17, 2022 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzhin-dmitry/55a09e919e7a53218e1e6c8149d8004f to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/55a09e919e7a53218e1e6c8149d8004f to your computer and use it in GitHub Desktop.
import random
wins_not_changing_door = 0
wins_changing_door = 0
N = 1000000 #we run a bunch of experiments
for i in range(N):
doors = [0,0,0] #0 denotes a goat, 1 denotes a prize
prize = random.randint(0,2)
doors[prize] = 1
guess = random.randint(0,2) #first choose - random
positions_left = [i for i in range(3) if i != guess and i != prize] #these are the choise variants to not show the prize
shown = random.sample(positions_left,1)[0] #showrunner picks one position
position_for_change = random.sample([i for i in range(3) if i != guess and i != shown], 1)[0]#and we pick a position for a change
if doors[guess] == 1:
wins_not_changing_door += 1 #we would win if we would NOT change the door
if doors[position_for_change] == 1:
wins_changing_door += 1 #we would win if we would change the door
#and the final result:
print('Wins when not changing door:', wins_not_changing_door, '(', 100 * wins_not_changing_door/ N, "%)")
print('Wins when changing door:', wins_changing_door, '(', 100 * wins_changing_door/ N, "%)")
#-----------------Output example:----------------
#Wins when not changing door: 333512 ( 33.3512 %)
#Wins when changing door: 666488 ( 66.6488 %)
#------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment