Sheep door car work

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: I don't think the odds have changed.

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.

A: Switching options will have a higher chance of getting a car.

The probability of choosing a car before opening the door is 1/3, and the probability of choosing a sheep is 2/3.

After opening the door, change the choice. If the car was selected before, the final choice was a sheep, with a probability of 1/3; if a sheep was selected before, the final choice was a car, with a probability of 2/3.

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)

Answer: Without changing the choice, the probability of getting a sheep is 0.667138, and the probability of getting a car is 0.332862.

       Change the choice, the probability of getting a sheep is 0.333491, and the probability of getting a car is 0.666509.

supported my analysis.

4. Please attach your code. (Hint: Use the Insert Code feature in the editor to display the code in Python style)

code show as below:

import random
a1=0 #Number of times to get sheep without exchange
b1=0 #Number of times to get a car without changing
a2=0 #Number of times to get sheep
b2=0 #Number of times to get a car
times=1000000
for i in range(times):
    changeless=random.randint(1,3) #Assume 1 is a sheep, 2 is a sheep, and 3 is a car
    if changeless==1 or changeless==2:
        a1 = a1 + 1
    elif changeless==3:
        b1=b1+1
for i in range(times):
    change=random.randint(1,3) #Assume 1 is a sheep, 2 is a sheep, and 3 is a car
    if change==1 or change==2:
        b2=b2+1
    elif change==3:
        a2=a2+1
print("Without changing the choice, the probability of getting a sheep is {}, and the probability of getting a car is {}.".format(a1/times,b1/times))
print("Change the choice, the probability of getting a sheep is {}, and the probability of getting a car is {}.".format(a2/times,b2/times))

  

Guess you like

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