Created
May 30, 2010 15:26
-
-
Save ybenjo/419103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Monty Hall Dilemma | |
class Monty | |
def initialize(i) | |
@door = [:goat, :goat, :car] | |
@trial_time = i | |
end | |
def first_choice | |
return rand(@door.size) | |
end | |
def open_index(player_index) | |
index = [0,1,2] | |
index.delete(player_index) | |
return @door[index[0]] == :goat ? index[0] : index[1] | |
end | |
def try(change = false) | |
index = [0,1,2] | |
@door.shuffle! | |
player_index = first_choice | |
if change | |
index.delete(player_index) | |
i = open_index(player_index) | |
index.delete(i) | |
player_index = index[0] | |
end | |
return @door[player_index] == :car ? 1 : 0 | |
end | |
def sim(flag) | |
sum = 0.0 | |
@trial_time.times do | |
sum += try(flag) | |
end | |
puts "change:#{flag} => #{sum/@trial_time*100}%" | |
end | |
end | |
if $0 == __FILE__ | |
m = Monty.new(100000) | |
m.sim(change = false) | |
m.sim(change = true) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment