{}
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. import numpy as np #The doors are numbered as 0,1,2. #We will do N trials N=100000 contestant_1_wins=0 contestant_2_wins=0 for _ in range(N): #The door with the prize is chosen at random by the host prize_door=np.random.randint(3) #The contestant chooses a random door chosen_door=np.random.randint(3) #The host randomly picks a door which does not hide the prize and opens it opened_door=np.random.choice([d for d in range(3) if d not in [chosen_door, prize_door]]) #The remaining door is losing. losing_door=[d for d in range(3) if d not in [opened_door, prize_door]][0] #The contestant switches doors if(chosen_door==prize_door): chosen_door=losing_door else: chosen_door=prize_door # A new contestant enters the game, and choses randomly between the two doors. new_contestant_door=np.random.choice([prize_door, losing_door]) # The prize door is revealed. if(chosen_door==prize_door): contestant_1_wins+=1. if(new_contestant_door==prize_door):contestant_2_wins+=1 print("Win probability for contestant 1: ", contestant_1_wins/N) print("Win probability for contestant 2: ", contestant_2_wins/N)
Output