mwittrock

monty.rb

Oct 28th, 2013
94
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×