Skip to content

Instantly share code, notes, and snippets.

@rafer
Created October 2, 2009 09:43
Show Gist options
  • Save rafer/199588 to your computer and use it in GitHub Desktop.
Save rafer/199588 to your computer and use it in GitHub Desktop.
# Should the contestant switch to the other door?
@switch = true
@wins = 0; @losses = 0
100000.times do
# Hide the car
car_behind = rand(3)
# Contestant picks a door
contestant_pick = rand(3)
# Host opens a door that does not have the prize and is not the contestant's pick
openable_doors = [0,1,2].delete_if{|x| x == contestant_pick or x == car_behind}
host_opens = openable_doors[rand(openable_doors.size)]
# Switch to the other door?
if @switch
new_pick = nil
3.times{|i| new_pick = i if i != contestant_pick and i != host_opens}
contestant_pick = new_pick
end
# See if the contestant won
car_behind == contestant_pick ? @wins += 1 : @losses += 1
end
puts "#{@wins} wins and #{@losses} losses (#{@wins.to_f / (@wins + @losses)}% win)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment