My question is related to sample code in 'Alogarithm R' section of this link http://ift.tt/1JFY8B3
// I copied below code snippet from that section // why this code is replacing elements with gradually decreasing probability? According to the problem each item in the input should have same probability right?
for i = k+1 to n
j := random(1, i)
if j <= k
R[j] := S[i]
For example compare Random function call for below three inputs with my reservoir size 10 1) random (1,15) chances are high for getting random numbers below 10 2) random (1, 100) chances are very low for getting random numbers below 10 3) random (1, 1000) chances are very very low for getting random numbers below 10
So chances of replacing items are very very less as input grows then how can we say that reservoir sampling algorithm is the solution for selecting random samples with equal probability on each Item? Mayou be I am missing some thing please explain.
Aucun commentaire:
Enregistrer un commentaire