Skip to content

Instantly share code, notes, and snippets.

View mwittrock's full-sized avatar

Morten Wittrock mwittrock

View GitHub Profile
@mwittrock
mwittrock / monty.rb
Created May 25, 2026 18:57
A Ruby simulation of the Monty Hall problem.
# This program simulates the Monty Hall problem (https://en.wikipedia.org/wiki/Monty_Hall_problem).
# Specifically, it counts how often the contestant wins by switching doors when given the choice
# by the game show host.
def contestant_wins?
doors = [:car, :goat, :goat]
indexes = 0...doors.size
player_first_choice = rand(doors.size)
host_choice = (indexes.select {|i| i != player_first_choice && doors[i] == :goat}).sample
player_second_choice = indexes.find {|i| i != player_first_choice && i != host_choice}