daily pastebin goal
19%
SHARE
TWEET

monty.rb

mwittrock Oct 28th, 2013 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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}"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top