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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律