给定n个优惠券,你期望在有替换的情况下抽多少张优惠券才能至少抽到每种优惠券一次。
我提出了两段代码:
(1)
n = 50
coupon = np.arange(0, n)
def collect(coupon):
number = 49
collection = np.array([])
while len(set(collection)) != n:
number +=1
collection = np.random.choice(coupon, replace = True, size = number)
return number
使用collect(coupon)进行10次迭代后,得到的平均值为:
[175. 151. 128. 132. 169. 118. 134. 138. 150. 135.]
143.0
(2)
n = 50
coupon = np.arange(0,n)
def collect(coupon):
collection = set()
number = 0
while len(collection) != n:
number +=1
got = np.random.choice(coupon)
collection.add(got)
return number
运行10次collect(coupon)并取平均值的结果为:
[184, 119, 286, 196, 172, 370, 163, 267, 238, 199]
219.4
我尝试了大量的迭代,代码(1)和代码(2)产生非常不同的结果。
我知道收集所有50个优惠券的期望值的正确答案是225,而代码(2)是正确的。另一方面,我找不到一个合理的解释为什么代码(1)失败了?为什么numpy.random.choice在这个例子中不起作用?