Created
July 25, 2014 20:18
-
-
Save tinco/a777f98f110b1f9773e8 to your computer and use it in GitHub Desktop.
Monty Hall playing code
This file contains 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
def play_monty_hall(strategy) | |
doors = [:car,:goat,:sheep] | |
guess = doors.sample | |
if strategy == :switch | |
# the revealed door is one that's not the winning and not the one we guessed | |
revealed_door = doors.reject{|door| door == :car || door == guess}.sample | |
# we change our guess to the door that was not revealed (and not our guess) | |
guess = doors.reject{|door| door == guess || door == revealed_door }.first | |
end | |
if guess == :car | |
:win | |
else | |
:lose | |
end | |
end | |
wins = 0 | |
1000.times do | |
wins += 1 if play_monty_hall(:switch) == :win | |
end | |
puts "When we switched we: " | |
puts " won #{wins} times" | |
wins = 0 | |
1000.times do | |
wins += 1 if play_monty_hall(:stick) == :win | |
end | |
puts "When we sticked we: " | |
puts " won #{wins} times" |
This file contains 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
phusion@tinco:~ $ ruby monty_hall.rb | |
When we switched we: | |
won 688 times | |
When we sticked we: | |
won 324 times | |
phusion@tinco:~ $ ruby monty_hall.rb | |
When we switched we: | |
won 664 times | |
When we sticked we: | |
won 311 times | |
phusion@tinco:~ $ ruby monty_hall.rb | |
When we switched we: | |
won 661 times | |
When we sticked we: | |
won 345 times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment