Skip to content

Instantly share code, notes, and snippets.

@hibariya
Created January 31, 2021 13:13
Show Gist options
  • Save hibariya/d0cfb079563a9b5a5ffb137cdfab3910 to your computer and use it in GitHub Desktop.
Save hibariya/d0cfb079563a9b5a5ffb137cdfab3910 to your computer and use it in GitHub Desktop.
Try Monty Hall problem a lot of times
def doit(change:, doors: [1, 2, 3])
jackpot = doors.sample
chosen = doors.sample
rest = (doors - [chosen])
drop = (rest - [jackpot]).first
finally_chosen =
if change
(rest - [drop]).first
else
chosen
end
finally_chosen == jackpot
end
n = 1_000_000
win_change = (n.times.count { doit(change: true) }).to_f / n
win_nochange = (n.times.count { doit(change: false) }).to_f / n
p win_change # 0.665929
p win_nochange # 0.332476
p win_change + win_nochange # 0.998405
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment