检索k我们使用一种称为水库采样的技术从不确定大小的数组中获取随机数。有人可以用示例代码简要介绍一下它是如何发生的吗?
我实际上没有意识到这个有一个名字,所以我从头开始证明并实现了这个:
def random_subset(iterator, K):
result = []
N = 0
for item in iterator:
N += 1
if len(result) < K:
result.append(item)
else:
s = int(random.random() * N)
if s < K:
result[s] = item
return result
From: http://web.archive.org/web/20141026071430/http://propersubset.com:80/2010/04/choosing-random-elements.html http://web.archive.org/web/20141026071430/http://propersubset.com:80/2010/04/choosing-random-elements.html
接近尾声时有证据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)