Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://en.wikipedia.org/wiki/Monty_Hall_problem
- def contestant_wins?
- doors = [:car, :goat, :goat].shuffle!
- indexes = 0..doors.size
- first_player_choice = rand(doors.size)
- host_choice = (indexes.select {|i| i != first_player_choice && doors[i] == :goat}).sample
- second_player_choice = indexes.find {|i| i != first_player_choice && i != host_choice}
- doors[second_player_choice] == :car
- end
- experiments = 1_000_000
- wins = 0
- experiments.times {wins +=1 if contestant_wins?}
- puts "Relative frequency of winning when switching doors: #{wins.to_f/experiments.to_f}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement