#!/usr/bin/pythonimportsys,randomrounds=10000wins=0random.seed()# The "-n" commandline option makes us run without ever switching.iflen(sys.argv)>1andsys.argv[1]=="-n":swap=Falseelse:swap=Trueforiinxrange(rounds):# Generate random door contentsdoors=["goat","goat","car"]random.shuffle(doors)# Pick a doordoor_choice=random.randrange(3)print"Selecting door",door_choice# Show a door with a goatforj,contentsinenumerate(doors):ifj!=door_choiceandcontents=="goat":goat_door=jprint"The host reveals a goat behind door",goat_doorbreakifswap:# Swap to the other doorforj,contentsinenumerate(doors):ifj!=door_choiceandj!=goat_door:swap_to=jprint"Swapping to door",swap_toelse:swap_to=door_choiceifdoors[swap_to]=="car":wins+=1print"You won the car!!"else:print"Sorry, but you're stuck with a goat"print"You played",rounds,"rounds, and won",wins,"of them!"

data mh;
do i = 1 to 100000;
prize = rand("TABLE",.333,.333);
* Monty puts the prize behind a random door;
initial_guess = rand("TABLE",.333,.333);
* We make a random initial guess;
* if the initial guess is right, Monte can open either of the others;
* which means that player can switch to either
of the other doors;
if initial_guess eq prize then do;
new_guess = initial_guess;
* choose a door until it's different from the initial guess-- that's the door we switch to;
do until (new_guess ne initial_guess);
new_guess = rand("TABLE",.333,.333);
end;
end;
* If the initial guess was wrong, Monte can rule out only one of the other doors;
* which means that we must switch to the right door;
if initial_guess ne prize then new_guess = prize;
output;
end;
run;
*;
data mh2;
set mh;
win_by_keep = (initial_guess eq prize);
win_by_switch = (new_guess eq prize);
run;
*;
proc means data = mh2 mean;
var win_by_keep win_by_switch;
run;