Last active
December 29, 2020 07:27
-
-
Save VioletVivirand/e48b615918d2c7e94edf97455f4c3caf to your computer and use it in GitHub Desktop.
三門問題模擬:換門跟不換門的中獎機率
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
ROUNDS = 10000 # The times to re-play | |
COUNT_CHANGE = 0 # If we change the chosen door, the times receiving the car | |
COUNT_NOT_CHANGE = 0 # If we don't change the chosen door, the times receiving the car | |
# Play the game, and choose to change the door after first picked | |
for _ in range(ROUNDS): | |
# Create 3 doors | |
doors = {0: 0, 1: 0, 2: 0} | |
# Mark one door as the grand prize | |
doors[random.randint(0, 2)] = 1 | |
# print("The answer is door #{}".format(str(doors))) | |
# Pick a door | |
pick = random.randint(0, 2) | |
# print("I pick door #{}".format(str(pick))) | |
doors.pop(pick) | |
# Open a door has a goat and it's not my pick | |
goat = list(filter(lambda k: doors[k]==0, doors))[0] | |
# print("Another door #{} has a goat!".format(str(goat))) | |
doors.pop(goat) | |
last_door = list(doors.keys())[0] | |
# print("I change my mind and pick another door #{}".format(str(last_door))) | |
if doors[last_door] == 0: | |
pass | |
# print("The last door #{} has a goat. Booooooooo".format(str(last_door))) | |
else: | |
COUNT_CHANGE += 1 | |
# print("The last door #{} has a Car. YAAAAAAAA!".format(str(last_door))) | |
# Play the game, and not to change the picked door | |
for _ in range(ROUNDS): | |
# Create 3 doors | |
doors = {0: 0, 1: 0, 2: 0} | |
# Mark one door as the grand prize | |
doors[random.randint(0, 2)] = 1 | |
# print("The answer is door #{}".format(str(doors))) | |
# Pick a door | |
pick = random.randint(0, 2) | |
# I'm not gonna change my mind, so just verify the answer | |
if doors[pick] == 0: | |
pass | |
# print("The door #{} has a goat. Booooooooo".format(str(pick))) | |
else: | |
COUNT_NOT_CHANGE += 1 | |
# print("The door #{} has a Car. YAAAAAAAA!".format(str(last_door))) | |
# Print the times we get the car | |
print(COUNT_CHANGE, COUNT_NOT_CHANGE) # Change my mind / Not change my mind | |
print(COUNT_CHANGE / COUNT_NOT_CHANGE) # Print ratio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
ROUNDS = 10000 # The times to re-play | |
COUNT_CHANGE = 0 # If we change the chosen door, the times receiving the car | |
COUNT_NOT_CHANGE = 0 # If we don't change the chosen door, the times receiving the car | |
# Play the game, and choose to change the door after first picked | |
for _ in range(ROUNDS): | |
# Create 3 doors | |
doors = {0: 0, 1: 0, 2: 0} | |
# Mark one door as the grand prize | |
doors[random.randint(0, 2)] = 1 | |
# Pick a door | |
pick = doors.pop(0) | |
if pick == 1: # If the first pick is a car, change a door must get a goat, just do nothing. | |
pass | |
else: | |
goat = list(filter(lambda k: doors[k]==0, doors))[0] # Filter out a remaining door that has a goat | |
doors.pop(goat) # Remove that door | |
last_door = list(doors.keys())[0] # The last door | |
if doors[last_door] == 0: # If the last door has a goat | |
pass | |
else: # If the last door has the car | |
COUNT_CHANGE += 1 | |
# Play the game, and not to change the picked door | |
for _ in range(ROUNDS): | |
# Create 3 doors | |
doors = {0: 0, 1: 0, 2: 0} | |
# Mark one door as the grand prize | |
doors[random.randint(0, 2)] = 1 | |
# Pick a door, and I won't change my mind, so just verify the answer | |
if doors[pick] == 0: | |
pass | |
else: | |
COUNT_NOT_CHANGE += 1 | |
# Print the times we get the car | |
print(COUNT_CHANGE, COUNT_NOT_CHANGE) # Change my mind / Not change my mind | |
print(COUNT_CHANGE / COUNT_NOT_CHANGE) # Print ratio |
最終:有換門而得到汽車的機率,將是不換門的兩倍,可以得到汽車的機率分別是 2/3 跟 1/3。
Oh my hands slipped and accidentally made this public 💫 😵 💫
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
v2
跟原版的差別:選擇的門 (pick
) 不用做隨機。因為已經隨機指定一個門裡有車子了,接下來可以直接指定一個門就好,再隨機也沒有多大的意義。也就是:
這兩件事情只做一件就好 😄
原版的做法比較直覺,就是照著綜藝節目的流程運作,而
v2
的方法就是單純求出結果。