Monty Hall Problem

本文通过Python模拟了著名的蒙提霍尔问题,探讨了在游戏节目中选择坚持最初选择、随机更换选择或是总是更换选择这三种策略下获胜的概率。实验结果显示,采取不同的策略将直接影响最终的胜率。

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值