Created
January 31, 2021 13:13
-
-
Save hibariya/d0cfb079563a9b5a5ffb137cdfab3910 to your computer and use it in GitHub Desktop.
Try Monty Hall problem a lot of times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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