Sheep door problem (Tan Yaodan)

Analysis of the problem of sheep door

 

         Title description: There are 3 closed doors, behind one door is parked cars, behind the rest are goats, only the host knows what is behind each door. Contestants can choose a door, and before opening it, the host will open another door, revealing the goat behind the door, and then allow the contestant to change their choice.

 

The homework requirements are as follows. You need to answer the following questions in the blog post. When answering a question, copy the question first, and then write the answer on a new line:

 

1. Answer according to your first feeling. Do you think you have a higher chance of getting a car if you don’t change the choice, or do you think you can get a car with a choice? Or the odds haven't changed?

 

A: At that time, I thought that the probability of getting a car must be high if you choose not to replace it, because the probability of getting a car at the beginning is 1/3. If it is replaced, the probability will not be so high. So when the teacher said, "Students who think they have a higher chance of getting a car if they don't change, raise their hands." My raised hand trembled slightly. . . .

 

 

2. Please carefully analyze "Is there a higher chance of getting a car if I don't change the choice, or is there a higher chance of getting a car if I change the choice? Or the probability has not changed?" Write down your analysis ideas and results.

 

Answer: ① This is the case where the contestant does not change his choice. If the contestant believes his intuition and does not change his choice, the probability of getting a car is 1/3; ② This is the case when the contestant changes his choice. If the car door is selected (probability 1/3) and changed, the car will definitely not be obtained in the end, so this possibility is ruled out; ③ If the contestant selected the sheep door for the first time (probability 2/3) and chose to replace (After changing the choice, the probability of selecting the door is 1/2), the total probability of selecting the car door in this case is 1/3; um, this is very embarrassing, if my analysis is correct, carefully analyze a After the wave, the probability of winning the car without changing the choice has not changed. What the hell! ! !

 

 

3. Please try to write a program to verify your ideas. The verification results support your analysis results, or do not support your analysis results. Please write the program running results and whether they support your analysis. (Hint: This procedure can be done with the help of the random number function)

 

A:  Well, let's talk nonsense first. I think I have failed to do this sheep door problem. After reading the title of this question, I didn't think about directly typing on the keyboard to calculate the probability, but thinking about writing code to simulate the process of interaction between the contestant and the host, and then calculating Probability, no way This is my LZ's problem-solving idea. Personally, I feel that it is really hard, and I have to try and make mistakes little by little. Then calculate the probability on this basis. In the process of simulating the interaction between the contestants and the host, I, a python novice, learned random.sample(list,n) to randomly obtain n elements in the list list to form fragments. Just combine the comments next to the code to understand my LZ's idea. It is really hard to type (xie) the code (BUG). It is already early in the morning. to be resolved.

 

4. My code.

Well, let's look at the first code first. This is not used to calculate the probability. It simulates the whole process of the interaction between the contestant and the host. If you don't like it, don't spray it. If there is any error, please point it out in the comment area.

 

1  #Assume there are 3 doors, the serial numbers are 1, 2, 3, and the door is opened according to the numbers entered by the contestants. 
2  # Assume that the user input is very friendly and will not enter illegally, so Xiaobai's code is not robust 
3  # coding : utf-8 
4  import random as rd #Introduce random numbers 
5  print ( " There are 3 closed doors, the serial numbers are 1, 2, 3, and there is a car parked behind one door, and a goat behind the other doors " )
 6 l = [1,2,3] #The elements in the list l represent 3 gates 
7 car = rd.randint(1,3) #Generate random numbers between the three gates 1,2,3 
8 x = eval ((input( " Please select the door the contestant wants to open: " )))   # x is the door selected by the contestant 
9 l.remove(x) # The host will open the door selected by the contestant before opening the door another door
10  if x != car: #Prevent the case where the door selected by the contestant is exactly the car door, but x is no longer in the list 
11      l.remove(car) #The host already knows which is the car door, and according to the meaning of the question, the first time It will not open that car door, but will open a sheep door 
12 s = rd.sample(l,1) #Get 5 elements randomly from the list l and return it as a fragment s, which can prevent x=car, the list There are also two values ​​in l 
13  for i in s:   # i is the door the host wants to open 
14      y = i # y is the door the host opens for the first time 
15      print ( "The host opened the first door {}door, revealing the goat behind the door " .format(y)) #The host opens the so-called other door, revealing the goat behind the door 
16 l = [1,2,3]     #Elements in the list l Represents 3 gates 
17 n = input( "Whether the contestant changes the choice: 1. Change the choice 2. Don't change, trust your intuition " )
 18  if n == 1:   #If the contestant changes the choice 19      l.remove(x) #The contestant requests to change the choice, so no It is possible to get the number x 20 that was originally entered in the list l      l.remove(y) #The host has already opened the door, so it is impossible to get 21 again for j in l: # j is the competition The door he chose after the contestant changed his choice 22 print ( "The contestant changed his choice, and the host opened the door {} selected by the contestant " .format(j))
 23 if j == car:   #Judging the competition Whether the door finally selected by the contestant is the car door 24 print ( "The contestant got the car " )
 25


     
              
              else :
 26          print ( " Unfortunately, there is a goat behind this door " )
 27  if n == 2:   #If the contestant did not change their choice, trust their intuition 
28      print ( "The contestant did not change their choice, the host opened The {}th door chosen by the contestant " .format(x))
 29  if x == car:
 30      print ( "The contestant got the car " )
 31  else :
 32      print ( " Unfortunately, there is a goat behind this door " )

 

Let's look at the code for calculating the probability. In order to save the calculation time, I only calculated the probability of winning the car after changing the choice, because the opposite of it must be the probability of winning the car without changing the choice.

 

1  #Assume there are 3 doors, the serial numbers are 1, 2, 3, and the door is opened according to the numbers entered by the contestants. 
2  # Assume that the user input is very friendly and will not enter illegally, so Xiaobai's code is not robust 
3  # coding : utf-8 
4 import  random as rd #Introduce random numbers 
5 times = 100000 #Define   the number of cycles here to facilitate the replacement of the number of cycles6 count = 0      #Use total to record the number of times the car is drawn after the contestants change their choices7 print ( " It is known that there are 3 closed doors with serial numbers 1, 2, 3, a car is parked behind one door, and a goat is behind the other doors " )
 8 car = rd.randint(1,3) # in 1,2 ,3 Generate random numbers between these three doors 9 for k in range(times):
 10      l = [1,2,3]

 
 #The elements in the list l represent 3 doors 
11      x = rd.choice(l) # x is the door chosen by the contestant 
12      for a in range(1,4 ):
 13          if a == x:
 14              l.remove (a) #The host will open another door before opening the door selected by the contestant15 
if a != car: #Prevent the              case where the door selected by the contestant is exactly the car door, but x is no longer in the list16                  l. remove(car) #The host already knows which door is the car, and according to the meaning of the question, the car door will not be opened for the first time, but a sheep door will be opened 17      s = rd.sample(l,1) #From the list l Randomly obtain 5 elements in the list and return them as a fragment s, which can prevent when x=car, there are still two values ​​18 in the list l for i in s:   #


     i is the door that the host wants to open 
19          y = i # y is the door that the host opens for the first time, revealing the goat behind the door 
20      l = [1,2,3]     #The elements in the list l represent 3 gates 
21      n = rd.randint(1,2) #Whether the contestant chooses to change: 1. Change the choice 2. Don't change, trust your intuition 
22      if n == 1:   #If the contestant changes the choice 
23          l. remove(x) #The contestant asks to change the choice, so it is impossible to get the number that was entered in the list l x 
24          l.remove(y) #The host has already opened the door, so it is impossible Then get 
25          for j in l: # j is the door that the contestant chooses after changing the choice 
26              if j == car:   # Determine whether the door the contestant finally chooses is the car door 
27                 count += 1 #Participant will win the car after changing the choice 
28 p1 = count / times #The probability that the participant will win the car after changing the choice is 
29 p2 = 1 - p1 #The probability that the participant will not change the choice to win the car 
30  print ( " The probability of winning a car after changing the choice is {}, and the probability of winning a car without changing the choice is {} " .format(p1,p2))

Aah, I finally finished writing. It's 1:51 and it's almost 2:00. I can finally go to bed. Although tomorrow's first session will be a physical test, I feel sorry for my roommate, and the tea shaft is clicking. . . .

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324992831&siteId=291194637