0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

モンティ・ホール問題が納得いかなかったので、モンテカルロシミュレーションしてみた(3)

Posted at

更に続き

モンティ・ホール問題が納得いかなかったので、モンテカルロシミュレーションしてみた
[モンティ・ホール問題が納得いかなかったので、モンテカルロシミュレーションしてみた(2)]
(https://qiita.com/g667408/items/69d4638d395e953af606)

montyがどのドアに賞品が入っているかを知らなかった場合には、ドアを変更しても確率は変わらないなかった。
それでもやはり納得がいかない。
じゃあ、参加者とmontyがドアを選んでから、その後に賞品のあるドアが決まったらどうなるのだろうか?

早速、試した。

import random
import sys

# 引数
try:
    n = int(sys.argv[1])
except:
    n = 1000 #引数が無い場合は1000回に

# 賞を得るのに、成功すると1、失敗すると0を返す
# changeドアを変更するか
# True:変更する
# False:変更しない
def monty(change):
    #参加者がドアを選ぶ
    select = random.randint(0,2)

    #続いてmontyがドアを選ぶ
    while True:
        monty_select = random.randint(0,2)
        if monty_select != select:#参加者が選ばなかったドアを選ぶ
            break            
    #montyが選ばなかったドアのどちらかに賞品を配置する。
    while True:
        prize = random.randint(0,2)
        if monty_select != prize:
            break            
    
    if change:#ドアを変更する場合
        while True:
            select_change = random.randint(0,2)
            if monty_select != select_change:
                select = select_change
                break
    if select == prize:#賞を得られたら1を返す
        return 1
    return 0#賞が得られなければ0を返す

change = 0
nochange = 0
for i in range(n):#指定回数を繰り返す
    change += monty(change = True)
    nochange += monty(change = False)

# 結果の表示
print("turn",n)
print("change  ",change)
print("nochange", nochange)

実行結果

参加者が選ぶドアを変えても変えなくても、参加者が賞品をもらえる確率はほぼ同じ50%になりました。

turn 1000
change   527
nochange 492
turn 1000
change   514
nochange 515
turn 1000
change   502
nochange 505

やっぱりmontyが賞品のドアを知っているかどうかが、問題の鍵になるんですね。
うーん。難しい。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?