possible_doors = [:goat, :goat, :car].permutation.to_a

bad_choice = 0

tries = 100000

1.upto tries do |mc|

test_doors = possible_doors[Random.rand(possible_doors.length)]

bad_choice += 1 if test_doors[Random.rand(test_doors.length)] == :car

end

puts "bad_choice percent = #{bad_choice.to_f / tries.to_f * 100} good_choice percent = #{100.0 - (bad_choice.to_f / tries.to_f * 100)}"

First up, we get a list of all the possible ways the doors could be arranged and set the number of times that switching would be a bad choice to 0. We'll run this 100000 times in a Monte (not Monty) Carlo loop. In the loop we get one of the possible arrangements of test doors randomly selected and then increment the bad_choice variable if the door we pick contains the car (in other words switching from where we are would be a "bad choice"). When we run this program we see that switching is a bad choice only about 33% of the time and a good choice about 66% of the time (one might guess that these are really 33.333333...% and 66.6666666%). So, then answer in this case is that yes you should switch.

Let me know if you have questions or comments.

## No comments:

## Post a Comment