Skip to content

Instantly share code, notes, and snippets.

@alexgriff
Created March 17, 2016 04:28
Show Gist options
  • Save alexgriff/bd757fc8c9f4ac9710ef to your computer and use it in GitHub Desktop.
Save alexgriff/bd757fc8c9f4ac9710ef to your computer and use it in GitHub Desktop.
# Return an array of length n_simulations.
# Each value is a random number between 0 and @n_doors-1.
# The value represents the door the prize is behind
# or the door the contestant guessed.
@n_doors = 3
def simulate_prizedoors(n_simulations)
(0...n_simulations).map {rand(@n_doors)}
end
def simulate_guesses(n_simulations)
(0...n_simulations).map {rand(@n_doors)}
end
# The host must now open a door to reveal a goat.
# There are two cases:
# case 1. The contestant chose the door with the prize behind it.
# The host can reveal a goat behind any remaining door.
# case 2. The contestant chose a door with a goat behind it.
# The host can reveal a goat behind any door that is not
# the prize door or the guessed door.
#
# Returns an array of length n_simulations
# where each value represents the door the host opens
def show_a_goat(prize_doors, guesses)
(0...guesses.size).map do |i|
all_possible_doors = (0...@n_doors).to_a
guess_door = guesses[i]
prize_door = prize_doors[i]
# case 1
if guess_door == prize_door
all_possible_doors.delete(guess_door)
# case 2
else
all_possible_doors.delete(guess_door)
all_possible_doors.delete(prize_door)
end
choice = rand(all_possible_doors.size)
all_possible_doors[choice]
end
end
# If a contestant decides to switch their guess they
# can choose any door except for the door with the
# revealed goat or their first-guessed-door.
#
# Returns an array of length n_simulations
# where each value represents the door contestant
# switched their guess to.
def switch_guess(guesses, goat_doors)
(0...guesses.size).map do |i|
all_possible_doors = (0...@n_doors).to_a
guess_door = guesses[i]
goat_door = goat_doors[i]
all_possible_doors.delete(guess_door)
all_possible_doors.delete(goat_door)
choice = rand(all_possible_doors.size)
all_possible_doors[choice]
end
end
# Calculate percentage where guess_door == prize_door
def win_percentage(guesses, prize_doors)
wins = guesses.zip(prize_doors).map{|pair| pair[0] <=> pair[1]}.select{ |elem| elem == 0}.size
100 * (wins.to_f / guesses.size)
end
# =======================
# Run the full simulation
# =======================
def run_simulation(n_simulations)
prize_doors = simulate_prizedoors(n_simulations)
guesses = simulate_guesses(n_simulations)
revealed_doors = show_a_goat(prize_doors, guesses)
switched_guesses = switch_guess(guesses, revealed_doors)
no_switch_win = win_percentage(guesses, prize_doors)
switch_win = win_percentage(switched_guesses, prize_doors)
puts "When a contestant SWITCHED their inital guess they won the prize #{switch_win}% of the time."
puts "And when they stuck with their first choice they won the prize #{no_switch_win}% of the time."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment