Created
December 19, 2010 18:24
-
-
Save milandobrota/747561 to your computer and use it in GitHub Desktop.
Monty Hall Problem simulation written in Ruby. Usage: ruby monty_hall.rb <number of games>
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
class Show | |
attr_reader :games | |
def initialize | |
@games = [] | |
end | |
def first_pick(door) | |
@current_game = Game.new(door) | |
@current_game.first_pick(door) | |
end | |
def second_pick(door) | |
@current_game.second_pick(door, false) | |
@games << @current_game | |
end | |
def simulate(number_of_games) | |
number_of_games.times do | |
door = (?A + rand(3)).chr | |
first_pick door | |
second_pick door | |
end | |
number_of_games.times do | |
door = (?A + rand(3)).chr | |
eliminated = first_pick door | |
other_door = (['A', 'B', 'C'] - [eliminated, door]).first | |
second_pick other_door | |
end | |
score | |
end | |
def number_of_games_played | |
@games.size | |
end | |
def games_won | |
@games.select {|g| g.won? } | |
end | |
def number_of_games_won | |
games_won.size | |
end | |
def games_played_with_change | |
@games.select {|g| g.changed?} | |
end | |
def number_of_games_played_with_change | |
games_played_with_change.size | |
end | |
def games_won_with_change | |
@games.select {|g| g.won? && g.changed?} | |
end | |
def number_of_games_won_with_change | |
games_won_with_change.size | |
end | |
def games_played_without_change | |
@games.select {|g| !g.changed?} | |
end | |
def number_of_games_played_without_change | |
games_played_without_change.size | |
end | |
def games_won_without_change | |
@games.select {|g| g.won? && !g.changed?} | |
end | |
def number_of_games_won_without_change | |
games_won_without_change.size | |
end | |
def score | |
puts "total: #{number_of_games_won}/#{number_of_games_played} #{number_of_games_won.to_f/number_of_games_played}" | |
puts "change: #{number_of_games_won_with_change}/#{number_of_games_played_with_change} #{number_of_games_won_with_change.to_f/number_of_games_played_with_change}" | |
puts "no change: #{number_of_games_won_without_change}/#{number_of_games_played_without_change} #{number_of_games_won_without_change.to_f/number_of_games_played_without_change}" | |
end | |
end | |
class Game | |
def initialize(door) | |
@winning_door = pick_winning_door_randomly | |
if door == @winning_door | |
@g | |
end | |
end | |
def won? | |
@won | |
end | |
def changed? | |
@changed | |
end | |
def first_pick(door) | |
@first_door = door | |
candidates_for_elimination = ['A', 'B', 'C'] - [door, @winning_door] | |
candidates_for_elimination[rand(candidates_for_elimination.size)] | |
end | |
def pick_winning_door_randomly | |
(?A + rand(3)).chr | |
end | |
def second_pick(second_door, verbose=true) | |
@changed = (@first_door != second_door) | |
@won = @winning_door == second_door | |
return @won unless verbose | |
if @won | |
puts "The winning door is #{@winning_door}. Congrats, you won!" | |
else | |
puts "The winning door is #{@winning_door}. Sorry, try again." | |
end | |
@won | |
end | |
end | |
Show.new.simulate ARGV[0].to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment