Skip to content

Instantly share code, notes, and snippets.

@kristopolous
Created September 12, 2012 00:36
Show Gist options
  • Save kristopolous/3703293 to your computer and use it in GitHub Desktop.
Save kristopolous/3703293 to your computer and use it in GitHub Desktop.
Monty Hall Simulation done in readable ruby.
#!/usr/bin/ruby
$door_count = 3
$door_list = 0.upto($door_count - 1).to_a
$game_count = 10 * 1000
def percent(numerator, denominator)
return "%5.2f%" %[100 * numerator.to_f / denominator.to_f]
end
puts "Door Count: #{$door_count}"
def coin_monty
puts "Coin Flip simulation: There may be 0-#{$door_count} goats or prizes. We flip a 2 sided coin to determine it."
switch_win = 0
stay_win = 0
$game_count.times {
prizeList = []
# We toss a coin and then add a prize to the door
# if the coin is heads (1)
0.upto($door_count - 1) { | door |
prizeList << door if rand(2) == 1
}
# You pick a random dooor.
start = rand($door_count)
doors = $door_list
# Monty dismisses a losing door.
if (doors - prizeList).length > 0
dismiss = (doors - (prizeList + [start])).choice
else
# or a winning door if there are no losing doors.
dismiss = (doors - [start]).choice
end
doors -= [start, dismiss]
# The door you switch to will be the one that
# wasn't dismissed and that wasn't the one that
# you started with.
switch = doors.first
# If you switched.
switch_win += 1 unless prizeList.index(switch).nil?
# puts "(%s) %s" %[prizeList.join(', '), start.to_s]
# If you stayed.
stay_win += 1 unless prizeList.index(start).nil?
}
puts "After #{$game_count} games:"
puts "%28s %s" %[ "switching won the prize:", percent(switch_win, $game_count)]
puts "%28s %s" %[ "staying won the prize:", percent(stay_win, $game_count)]
puts ""
end
def rude_monty
puts "Rude Monty simulation: Monty will never pick the prize but may pick your door, in which you move over your initial door choice."
switch_win = 0
stay_win = 0
$game_count.times {
# Prize is behind a random door.
prize = rand($door_count)
# You pick a random dooor.
start = rand($door_count)
doors = $door_list
# Monty dismisses a losing door.
dismiss = (doors - [prize]).choice
# If that was my door, I have to switch
if dismiss == start
start = (doors - [dismiss]).choice
end
doors -= [start, dismiss]
# The door you switch to will be the one that
# wasn't dismissed and that wasn't the one that
# you started with.
switch = doors.first
# If you switched.
switch_win += 1 if switch == prize
# If you stayed.
stay_win += 1 if start == prize
}
puts "After #{$game_count} games:"
puts "%28s %s" %[ "switching won the prize:", percent(switch_win, $game_count)]
puts "%28s %s" %[ "staying won the prize:", percent(stay_win, $game_count)]
puts ""
end
def smart_monty
puts "Smart Monty simulation: Monty will never pick the prize."
switch_win = 0
stay_win = 0
$game_count.times {
# Prize is behind a random door.
prize = rand($door_count)
# You pick a random dooor.
start = rand($door_count)
doors = $door_list
# Monty dismisses a losing door.
dismiss = (doors - [start, prize]).choice
doors -= [start, dismiss]
# The door you switch to will be the one that
# wasn't dismissed and that wasn't the one that
# you started with.
switch = doors.first
# If you switched.
switch_win += 1 if switch == prize
# If you stayed.
stay_win += 1 if start == prize
}
puts "After #{$game_count} games:"
puts "%28s %s" %[ "switching won the prize:", percent(switch_win, $game_count)]
puts "%28s %s" %[ "staying won the prize:", percent(stay_win, $game_count)]
puts ""
end
def dumb_monty
puts "Dumb Monty simulation: Monty may pick the prize."
switch_win = 0
stay_win = 0
loss_count = 0
played_on_count = 0
$game_count.times {
# Prize is behind a random door.
prize = rand($door_count)
# You pick a random dooor.
start = rand($door_count)
# Monty dismisses a random door, which may have the prize.
dismiss = ($door_list - [start]).choice
# If it had the prize, then you lose.
if (dismiss == prize)
loss_count += 1
else
# Otherwise, we continue.
played_on_count += 1
# The door you switch to will be the one that
# wasn't dismissed and that wasn't the one that
# you started with.
switch = ($door_list - [start, dismiss]).first
# If you switched.
switch_win += 1 if switch == prize
# If you stayed.
stay_win += 1 if start == prize
end
}
puts "After #{$game_count} games:"
puts " You lost, because Monty picked the prize: #{percent(loss_count, $game_count)}"
puts ""
puts "%28s %s" %[ "switching won the prize:", percent(switch_win, played_on_count)]
puts "%28s %s" %[ "including times lost:", percent(switch_win, $game_count)]
puts ""
puts "%28s %s" %[ "staying won the prize:", percent(stay_win, played_on_count)]
puts "%28s %s" %[ "including times lost:", percent(stay_win, $game_count)]
puts ""
end
smart_monty
rude_monty
dumb_monty
coin_monty
@kalensk
Copy link

kalensk commented Sep 12, 2012

Smart Monty simulation: Monty will never pick the prize.
After 100000 games:
staying won the prize: 33.43%

Dumb Monty simulation: Monty may pick the prize.
After 100000 games:
Monty picked the prize: 33.52%

switching won the prize: 50.16%
   including times lost: 33.35%

  staying won the prize: 49.84%
   including times lost: 33.13%

@kalensk
Copy link

kalensk commented Sep 12, 2012

Smart Monty simulation: Monty will never pick the prize.
After 100000 games:

switching won the prize: 66.57%
staying won the prize: 33.43%

Dumb Monty simulation: Monty may pick the prize.
After 100000 games:

Monty picked the prize: 33.52%

switching won the prize: 50.16%
including times lost: 33.35%

staying won the prize: 49.84%
including times lost: 33.13%

@kristopolous
Copy link
Author

Door Count: 3
Smart Monty simulation: Monty will never pick the prize.
After 10000 games:
switching won the prize: 67.74%
staying won the prize: 32.26%

Rude Monty simulation: Monty will never pick the prize but may pick your door, in which you move over your initial door choice.
After 10000 games:
switching won the prize: 50.15%
staying won the prize: 49.85%

Dumb Monty simulation: Monty may pick the prize.
After 10000 games:
Monty picked the prize: 32.69%

switching won the prize: 50.20%
   including times lost: 33.79%

  staying won the prize: 49.80%
   including times lost: 33.52%

Coin Flip simulation: There may be 0-3 goats or prizes. We flip a 2 sided coin to determine it.
After 10000 games:
switching won the prize: 75.25%
staying won the prize: 49.94%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment