Skip to content

Instantly share code, notes, and snippets.

@Dzol
Dzol / monty.rb
Created June 1, 2026 10:11 — forked from mwittrock/monty.rb
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}