Skip to content

Instantly share code, notes, and snippets.

@Shikugawa
Created July 4, 2021 14:04
Show Gist options
  • Save Shikugawa/de9f55b0da697d764081d15a2821792c to your computer and use it in GitHub Desktop.
Save Shikugawa/de9f55b0da697d764081d15a2821792c to your computer and use it in GitHub Desktop.
モンティ・ホール問題のシミュレーション、ドアを変えたほうが当たる確率が高い
import random
doors = [True, False, False]
def get_false():
s = []
for i, d in enumerate(doors):
if not d:
s.append(i)
return s
def get_true():
s = []
for i, d in enumerate(doors):
if d:
return i
cnt = 0
for i in range(0, 100000):
random.shuffle(doors)
s = random.randint(0, 2)
while True:
m = random.randint(0, 1)
m = get_false()[m]
if m != s:
break
for i in range(0, 3):
if i != s and i != m:
s = i
break
if s == get_true():
cnt += 1
print(cnt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment