Skip to content

Instantly share code, notes, and snippets.

@thebinarypenguin
Created March 24, 2014 12:01
Show Gist options
  • Save thebinarypenguin/9738911 to your computer and use it in GitHub Desktop.
Save thebinarypenguin/9738911 to your computer and use it in GitHub Desktop.
An experimental proof of the Monty Hall problem in Ruby. Reference: http://en.wikipedia.org/wiki/Monty_Hall_problem
#!/usr/bin/env ruby
sample_size = 3000
stay_counter = 0
switch_counter = 0
sample_size.times do |i|
# Randomly create a scenario
winning_door = [1,2,3].sample
player_choice = [1,2,3].sample
revealed_door = ([1,2,3] - [winning_door, player_choice]).sample
# Determine the winning move
if player_choice == winning_door
winning_move = 'Stay'
stay_counter += 1
else
winning_move = 'Switch'
switch_counter += 1
end
# Display the results
puts "Winning Door: #{winning_door} " +
"Player Choice: #{player_choice} " +
"Revealed Door: #{revealed_door} " +
"Winning Move: #{winning_move}"
end
# Display the aggregate results
puts
puts "Wins by staying : #{stay_counter}/#{sample_size}"
puts "Wins by switching : #{switch_counter}/#{sample_size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment