通过昨天在课堂上尝试向朋友解释蒙蒂霍尔问题,我们最终用 Python 编写代码来证明如果你总是交换,你会赢 2/3 次。我们想出了这个:
import random as r
#iterations = int(raw_input("How many iterations? >> "))
iterations = 100000
doors = ["goat", "goat", "car"]
wins = 0.0
losses = 0.0
for i in range(iterations):
n = r.randrange(0,3)
choice = doors[n]
if n == 0:
#print "You chose door 1."
#print "Monty opens door 2. There is a goat behind this door."
#print "You swapped to door 3."
wins += 1
#print "You won a " + doors[2] + "\n"
elif n == 1:
#print "You chose door 2."
#print "Monty opens door 1. There is a goat behind this door."
#print "You swapped to door 3."
wins += 1
#print "You won a " + doors[2] + "\n"
elif n == 2:
#print "You chose door 3."
#print "Monty opens door 2. There is a goat behind this door."
#print "You swapped to door 1."
losses += 1
#print "You won a " + doors[0] + "\n"
else:
print "You screwed up"
percentage = (wins/iterations) * 100
print "Wins: " + str(wins)
print "Losses: " + str(losses)
print "You won " + str(percentage) + "% of the time"
我的朋友认为这是一个很好的方法(并且是一个很好的模拟),但我有我的怀疑和担忧。它真的足够随机吗?
我遇到的问题是所有选择都是硬编码的。
这是对蒙蒂霍尔问题的好还是坏的“模拟”?怎么来的?
你能想出一个更好的版本吗?