先把前k个数放入蓄水池,对于第 i>=k+1,我们以 k/i 概率决定是否要把它换入蓄水池,换入时随机的选取一个作为替换项,这样一直做下去,对于任意的样本空间n,对每个数的选取概率都为k/n。也就是说对每个数选取概率相等。
init a reservoir with the size k
add the first k elements into the reservoir
for i = k+1 to N
m = random(1,i);
if(m < k)
swap the m_th value and i_th value
end for
3932

被折叠的 条评论
为什么被折叠?



