#!/usr/bin/ruby -w
=begin
/***************************************************************************
* Copyright (C) 2008 by Paul Lutus *
* lutusp@arachnoid.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
=end
def monty_test(switch_doors)
# put the prize behind a random door
prize = rand(3)
# the contestant's random choice
choice = rand(3)
win = false
if(switch_doors)
# the teaser "Monty door" must be neither the
# contestant's choice nor the winning door
monty_door = 0
monty_door += 1 while (monty_door == choice || monty_door == prize)
# the contestant's new choice must not be
# his existing choice nor the Monty door
new_choice = 0
new_choice += 1 while (new_choice == choice || new_choice == monty_door)
win = (new_choice == prize)
else # don't switch doors
win = (choice == prize)
end
return win
end
def monty_game(max,switch_doors)
if(switch_doors)
print "Switching doors: "
else
print "Not switching doors: "
end
score = 0
1.upto(max) do
score += 1 if monty_test(switch_doors)
end
result = score.to_f / max.to_f
puts "win probability #{result}"
end
puts "Monty Hall Problem:"
max = 100000
monty_game(max,true)
monty_game(max,false)