Advertisement
mwittrock

monty.rb

Oct 28th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.58 KB | None | 0 0
  1. # http://en.wikipedia.org/wiki/Monty_Hall_problem
  2.  
  3. def contestant_wins?
  4.   doors = [:car, :goat, :goat].shuffle!
  5.   indexes = 0..doors.size
  6.   first_player_choice = rand(doors.size)
  7.   host_choice = (indexes.select {|i| i != first_player_choice && doors[i] == :goat}).sample
  8.   second_player_choice = indexes.find {|i| i != first_player_choice && i != host_choice}
  9.   doors[second_player_choice] == :car
  10. end
  11.  
  12. experiments = 1_000_000
  13. wins = 0
  14. experiments.times {wins +=1 if contestant_wins?}
  15.  
  16. puts "Relative frequency of winning when switching doors: #{wins.to_f/experiments.to_f}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement