题目:一个选手去参见TV秀,三扇门后面,只有一扇门有奖品,这扇门只有主持人知道。当选手选择一扇门之后,主持人会打开其余两扇门中的一扇空门,问选手是否坚持最初的选择。
请计算改变最初选择之后的中奖概率和坚持最初选择之后的中奖概率。
示例代码:
import numpy as np
import numpy.random as random
random.seed(42)
test_num = 10000
winning_doors = random.randint(0, 3, test_num)
changed_mind_num = 0
insist_num = 0
for winning_door in winning_doors:
first_try = random.randint(0, 3)
remaing_choices = [i for i in range(3) if i != first_try]
wrong_choices = [i for i in range(3) if i != winning_door]
if first_try in wrong_choices: #如果first_try在wrong_choices列表中
wrong_choices.remove(first_try)
screened_out = random.choice(wrong_choices)
remaing_choices.remove(screened_out)
changed_mind_try = remaing_choices[0]
changed_mind_num += 1 if changed_mind_try == winning_door else 0
insist_num += 1 if first_try== winning_door else