去钓鱼
Gone Fishing
题目详情
一只熊想要从河里捕获 鱼。钓到 3 条鱼后,熊就离开了。当鱼来时,他有 的机会抓住它。第五条鱼没被钓到的概率是多少?
A bear wants to catch fish from a river. Upon catching 3 fish the bear leaves. When a fish comes, there is a chance he'll catch it. What is the probability that the fifth fish will not be caught?
解析
只有当熊从第一条 中捕获 、 或 鱼时,它才会钓鱼第五条鱼。这是 的二项式: 我们知道,当熊钓鱼时,有一半的机会会钓到第五条鱼。这对应于概率 。然而,我们正在计算第五条鱼被 捕获的概率。第五条鱼未被捕获的概率只是该事件的补充,因此我们得到:
import random
catch_fish = lambda: True if random.randint(1, 2) == 1 else False
num_fifth_fish_caught = 0
num_iters = 100_000
for i in range(num_iters):
total_fish = 0
for fish in range(4):
if catch_fish():
total_fish += 1
if total_fish < 3:
if catch_fish():
num_fifth_fish_caught += 1
print((num_iters - num_fifth_fish_caught)/num_iters)Original Explanation
The bear will only be fishing for the fifth fish when it has caught , , or fish out of the first . This is binomial with :
We know the bear will catch the fifth fish half the time when it is fishing for it. This corresponds to the probability . However, we are looking to compute the probability that the fifth fish is caught. The probability the fifth fish is not caught is simply the complement of this event and so we get:
import random
catch_fish = lambda: True if random.randint(1, 2) == 1 else False
num_fifth_fish_caught = 0
num_iters = 100_000
for i in range(num_iters):
total_fish = 0
for fish in range(4):
if catch_fish():
total_fish += 1
if total_fish < 3:
if catch_fish():
num_fifth_fish_caught += 1
print((num_iters - num_fifth_fish_caught)/num_iters)