您的位置:首页 > 其它

Monty Hall Problem

2013-03-14 20:46 225 查看
import sys

import random as rnd

#strategy=sys.argv[1]# must be 'stick','choose',or 'switch'

def Solve(strategy):

wins = 0

for trail in range(100):

#The price is always in envelop 0... but we don't know that!

envelopes = [0,1,2]

first_chose = rnd.choice(envelopes)

if first_chose == 0:

envelopes = [0,rnd.choice([1,2])] #Randomly retain 1 or 2

else:

envelopes=[0,first_chose]# Retain winner and first choice

if strategy == 'stick':

second_choice = first_chose

elif strategy == 'choose':

second_choice= rnd.choice(envelopes)

elif strategy == 'switch':

envelopes.remove(first_chose)

second_choice = envelopes[0]

if second_choice ==0:

wins += 1

print strategy +':' + str( wins)

Solve('stick')

Solve('choose')

Solve('switch')

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: