Last active
February 7, 2021 13:49
-
-
Save asukaminato0721/4729fc66f1aa9f6c3680269e7df9d9f7 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
# 源代码来自 @Wind | |
# need py >= 3.8 | |
''' | |
已知一个黑箱里有10个红球、20个白球、30个蓝球。它们除了颜色之外,其它方面都一模一样。我们每次可以伸手进去取出一个球,当然自己看不到所取球的颜色。 | |
问:依次取球的时候,红球最先被取完的概率是多少? | |
''' | |
import random | |
noRedTime, noWhiteTime, noBlueTime = 0, 0, 0 | |
c = ["红"] * 10 + ["白"] * 20 + ["蓝"] * 30 | |
for __ in range(10): | |
for _ in range(100000): | |
random.shuffle(c) # 利用 shuffle 一次性打乱,提高效率 | |
r, w, b = 10, 20, 30 | |
for k in c: | |
if r and w and b: | |
r -= k == "红" | |
b -= k == "蓝" | |
w -= k == "白" | |
noRedTime += r == 0 | |
noBlueTime += b == 0 | |
noWhiteTime += w == 0 | |
print( | |
f"{noRedTime = }", | |
f"{noWhiteTime = }", | |
f"{noBlueTime = }", | |
f"{noRedTime / (noRedTime + noBlueTime + noWhiteTime) = }", | |
sep="\n", | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment