Created
March 24, 2014 12:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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