ZhangZhihui's Blog  

The problem can be formulated as follows. As a participant of a game show, you have to choose one of three doors. Behind one of the doors is a prize, behind two other doors is nothing. After you pick a door, the game host, who knows where the prize is, selects a door with no prize from the two remaining doors, and opens it. Then the host tells you that you may stick to your original choice or switch to another closed door. Should you do it?

 

Player A would always stick to the original choice, while Player B would always switch the door. 

 

复制代码
from random import choice


N = 100000

player_a_win = 0  # Palyer A sticks to the original choice.
player_b_win = 0  # Palyer B switches the door.


def monty_choice(prize, options):
    if prize[options[0]] == False and prize[options[1]] == False:
        return choice(options)

    return options[0] if prize[options[1]] == True else options[1]


for _ in range(N):
    prize = [False, False, False]
    prize[choice([0, 1, 2])] = True

    options = [0, 1, 2]
    player_choice = choice(options)
    options.remove(player_choice)
    options.remove(monty_choice(prize, options))

    if prize[player_choice]:
        player_a_win += 1
    if prize[options[0]]:
        player_b_win += 1


print(f"Player A: {player_a_win / N}")
print(f"Player B: {player_b_win / N}")
复制代码

 

Player A: 0.33323
Player B: 0.66677

 

Player B, who always switches doors, wins twice more often! 

If your intuition still rebels, here is an easy way to think about this game. Suppose you have initially chosen a door with the prize. The chances of this lucky event are 1 out of 3. In this case, switching doors means failure: you move away from your prize. However, in any other case (2 out of 3) you choose between your empty door and another closed door with the prize behind. Therefore, “always switch” strategy wins in 2/3 of all the games.

 

posted on   ZhangZhihuiAAA  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示