Let k = SIZE * r //SIZE is the size of input array
Let R be the result array (of size k), and S be the original (input) array
//first populate the first k elements or R with first k elements of S
for each i from 1 to k:
R[i] = S[i]
//then, choose randomly if and which element from R to replace with the new candidate
for each i from k+1 to SIZE:
j = random(1,i) //uniformly distributed number between 1 to i
//insert the new element at probability i/k, instead one of the existing elements
if j < k:
R[k] = S[i]
return R
1条答案
按热度按时间eqzww0vc1#
是的,一次传递的数据就足够用任何比例对其进行采样(随它去吧)
r
),使用水库取样:最后,每个元素都有
k/SIZE = r
待挑选