Monty Hall problem: Difference between revisions

Content added Content deleted
(Added a solution in Ruby)
Line 1,027: Line 1,027:
#random guess
#random guess
shown = guess = rand(3)
guess = rand(3)
#random door shown, but it is neither the guess nor the car
#random door shown, but it is neither the guess nor the car
shown = rand(3) while shown == guess || doors[shown] == :car
begin shown = rand(3) end while shown == guess || doors[shown] == :car
#staying with the initial guess wins if the initial guess is the car
#staying with the initial guess wins if the initial guess is the car
Line 1,040: Line 1,040:
end
end


printf "Staying wins %.2f%s of the time.\n" , 100.0 * stay / n , '%'
puts "Staying wins %.2f%% of the time." % (100.0 * stay / n)
printf "Switching wins %.2f%s of the time.\n" , 100.0 * switch / n , '%'</lang>
puts "Switching wins %.2f%% of the time." % (100.0 * switch / n)</lang>
Sample Output:
Sample Output:
<pre>Staying wins 33.84% of the time.
<pre>Staying wins 33.84% of the time.