My question is related to sample code in 'Algorithm R' section of this link https://en.m.wikipedia.org/wiki/Reservoir_sampling
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
- random (1,15) chances are high for getting random numbers below 10
- random (1, 100) chances are very low for getting random numbers below 10
- 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.