Skip to content

Instantly share code, notes, and snippets.

@aviatesk
Last active April 11, 2023 13:40
Show Gist options
  • Save aviatesk/3e0563868645d7dbabebed6071d2caaa to your computer and use it in GitHub Desktop.
Save aviatesk/3e0563868645d7dbabebed6071d2caaa to your computer and use it in GitHub Desktop.
Simulate (rather, just "write down") the Monty Hall problem.
function onegame()
doors = (1,2,3)
# drop(i::Int) = i == 1 ? (2,3) : i == 2 ? (1,3) : (1,2)
true_ans = rand(doors)
player1_ans = first_ans = rand(doors)
if first_ans == 1
if true_ans == 1
# open = rand(drop(1))
# [...]
return false # player1 wins
elseif true_ans == 2
open = 3
player2_ans = 2
return true # player2 wins
else
open = 2
player2_ans = 3
return true # player2 wins
end
elseif first_ans == 2
if true_ans == 1
open = 3
player2_ans = 1
return true # player2 wins
elseif true_ans == 2
# open = rand(drop(2))
# [...]
return false # player1 wins
else
open = 1
player2_ans = 3
return true # player2 wins
end
else
if true_ans == 1
open = 2
player2_ans = 1
return true # player2 wins
elseif true_ans == 2
open = 1
player2_ans = 2
return true # player2 wins
else
# open = rand(drop(3))
# [...]
return false # player1 wins
end
end
end
let n = 1000000
sum(onegame() for i = 1:1000000) / n # => 0.666129
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment