【发布时间】:2010-11-17 21:15:09
【问题描述】:
通过昨天在课堂上尝试向朋友解释Monty Hall problem,我们最终用 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"
我的朋友认为这是一个很好的方法(并且是一个很好的模拟),但我有我的怀疑和担忧。它真的足够随机吗?
我遇到的问题是所有选择都是硬编码的。
对于蒙蒂霍尔问题,这是一个好还是坏的“模拟”?怎么会?
你能想出一个更好的版本吗?
【问题讨论】:
-
你想达到什么目的?
-
米奇:一个准确的方法来证明你有 2/3 的获胜机会,前提是你交换门
-
数学证明。经验数据永远不能用作证明,它可以用作证据或支持。
标签: python language-agnostic probability