半失灵的口香糖机
Semi-Working Gum Machine
题目详情
你和另外两个朋友从半功能的口香糖机中获取口香糖。本机将独立均匀地弹出一段长度为区间的口香糖。在 3 根棍子中,你将收到最短的一根。你的口香糖棒的长度在 1 和 2 之间的概率是多少?
You and two other friends get gum from a semi-functioning gum machine. This machine will pop out a stick of gum with a length from the interval independently and uniformly. Of the 3 sticks, you will receive the shortest one. What is the probability the length of your gum stick is between 1 and 2?
解析
如果最小长度在 1 到 2 之间,则所有棍子的长度必须至少为 1。这种情况发生的概率为 。然而,这包括所有口香糖棒都长于 2 的情况。减去这种情况,我们得到的概率为:
import random
generate = lambda: random.uniform(0, 3)
valid_trial = 0
num_iters = 100_000
for i in range(num_iters):
a = generate()
b = generate()
c = generate()
smallest = min(a, b, c)
if smallest > 1 and smallest < 2:
valid_trial += 1
#expecting ~ 0.259
print(valid_trial/num_iters)Original Explanation
If the minimum length is between 1 and 2, all sticks must be at least 1 long. This occurs with probability . However, this includes the instance where all sticks of gum are longer than 2. Subtracting this case we get our probability to be:
import random
generate = lambda: random.uniform(0, 3)
valid_trial = 0
num_iters = 100_000
for i in range(num_iters):
a = generate()
b = generate()
c = generate()
smallest = min(a, b, c)
if smallest > 1 and smallest < 2:
valid_trial += 1
#expecting ~ 0.259
print(valid_trial/num_iters)