油藏取样
为了从大小未定的数组中检索k个随机数,我们使用了一种称为水库采样的技术。有人可以用示例代码简要介绍一下它是如何发生的吗?
To retrieve k random numbers from an array of undetermined size we use a technique called reservoir sampling. Can anybody briefly highlight how it happens with a sample code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我实际上没有意识到这个有一个名字,所以我从头开始证明并实现了这个:
来自: http://web.archive.org/web/20141026071430/http://propersubset.com:80/2010/04/choosing -random-elements.html
证明接近尾声。
I actually did not realize there was a name for this, so I proved and implemented this from scratch:
From: http://web.archive.org/web/20141026071430/http://propersubset.com:80/2010/04/choosing-random-elements.html
With proof near the end.
更严格地遵循 Knuth (1981) 的描述,油藏采样(算法 R)可以按如下方式实现:
Following Knuth's (1981) description more closely, Reservoir Sampling (Algorithm R) could be implemented as follows:
爪哇
Java
Python解决方案
Python solution