# define procedure to generate random integer from choices {1, 2, 3} four := rand(1..4); # define procedure to generate random integer from choices {1, 2, 3} three := rand(1..3): # define procedure to generate random integer from choices {1, 2} two := rand(1..2): # set the number of games in the simulation num_games := 1000; montyhall := proc(num_games) # initialize counters win_via_keep := 0: win_via_switch := 0: ########## BEGIN LOOP ########## for game_count from 1 to num_games do # shuffle one prize and two goats behind doors 1, 2 and 3 prize := three(); # contestant makes first choice of three doors c1 := three(); # host reveals a goat behind one of the doors NOT chosen if c1 = prize then # select one of other two goats at random choices := {1, 2, 3} minus {prize}; host := choices[two()]; else # must select the other goat choices := {1, 2, 3} minus {prize, c1}; host := choices; fi; # contestant elects to switch choices := {1, 2, 3} minus {c1, host}; c2 := choices; # tabulate wins according to strategy if c1 = prize then win_via_keep := win_via_keep + 1; else win_via_switch := win_via_switch + 1; fi; ########## END LOOP ########## od: # compute empirical probabilities prob_win_via_keep := evalf(win_via_keep/num_games); prob_win_via_switch := evalf(win_via_switch/num_games); print('prob_win_via_keep',prob_win_via_keep,' prob_win_via_switch',prob_win_via_switch); # total probabilities will not be one #total_prob := prob_win_via_keep + prob_win_via_switch; end;