python羊车门问题的正确解答

羊车门问题

羊车门问题描述:有3扇关闭的门,一扇门后停着汽车,另外两扇门后是山羊,主持人知道每扇门后是什么。参赛者首先选择一扇门。在开启它之前,主持人会从另外两扇门中打开一扇门,露出门后的山羊。此时,允许参赛者更换自己的选择。请问,参赛者更换选择后,能否增加猜中汽车的机会?通过设计并编写程序验证,并给出自己的解释。答案要求以如下方式给出。(The sheep door has 3 closed door, a door parked car, another two door is a goat, the host knows every door. What is the first choice of the contestants in the open door. Before it, the moderator will open a door from the other two doors, exposes the goat after. At this time, allow the participants change their choice. Please choose the contestants after replacement, can increase the chance of guessing car? Please through design and program verification, and gives his own interpretation.  )

1、我认为会增加选中汽车的机会。

原因如下:
(1)不换选择:选对的概率为1/3

(2)更换选择:

假设一开始选中的是车,主持人指出其中一只羊之后,那么剩下的一个门也必定是羊,那么此时更改选择必定是得不到车的,这是第一种;假如一开始选中的是羊,在主持人指出一只之后,无论如何更改选择都是可以得到车的,所以有两种情况更改是ok的,所以是2/3。

from random import*
TIMES = 10000
my_first_choice_n=0#初始化不改选择的次数
my_change_choice_n=0#初始化更改选择的次数
for i in range(TIMES):
	car_inDoor=randint(0,2)
	my_guess=randint(0,2)
	if car_inDoor==my_guess:
		my_first_choice_n+=1
	else:
		my_change_choice_n+=1
print("不改选择:{}".format(my_first_choice_n/TIMES))
print("更改选择:{}".format(my_change_choice_n/TIMES))

所以运行结果就是

猜你喜欢

转载自blog.csdn.net/honorwh/article/details/81810006
今日推荐