Given:
s = a[1]
for i = 2 -> n:
if randint(1, i) = i:
s = a[i]
return(s)P(s = a[i])
= P(a[i] gets picked) * P(a[i+1] doesn't get picked) * P(a[i+2] doesn't get picked) *...* P(a[n] doesn't get picked)
= P(a[i] gets picked) * {1 - P(a[i+1] gets picked)} * {1 - P(a[i+2] gets picked)} *...* {1 - P(a[n] gets picked)}
= (1/i) * (1 - 1/(i+1)) * (1 - 1/(i+2)) *...* (1 - 1/n)
= 1/i * i/(i+1) * (i+1)/(i+2) *...* (n-1)/n
= 1/ns[1] = a[1], s[2] = a[2]...s[k] = a[k]
for i = k+1 -> n:
j = randint(1, i)
if j <= k:
s[j] = a[i]
return(s)P(a[i] element of s)
= P(a[i] gets picked as s[j]) * P(a[i+1] doesn't swap s[j]) * P(a[i+2] doesn't swap s[j]) *...* P(a[n] doesn't swap s[j])
= P(a[i] gets picked as s[j]) * {1 - P(a[i+1] swaps s[j])} * {1 - P(a[i+2] swaps s[j])} *...* {1 - P(a[n] swaps s[j])}
= (k/i) * (1 - 1/(i+1)) * (1 - 1/(i+2)) *...* (1 - 1/n)
= k/i * i/(i+1) * (i+1)/(i+2) *...* (n-1)/n
= k/nNote: Probability of a[i] getting picked is k/i however, for a[i] to replace s[j] the probability is 1/i since there is only one choice out of i indices.