You have to choose an algorithm that can really simulate the actual "Randomly select k numbers from n numbers" activity. Your algorithm must have two properties
(1) He must return k numbers at the end.
(2) He should really imitate these properties of the target activity: each number is selected with probability k / n.
The rim s answer is wrong because it hasn t the first property.
for i = 0 to n randomly choose an integer number between [1,n-i+1] if [randomValue <= (k - S'.size)/(S.size - i + 1)] then S'.add(S[i])
Using the above plan, each number is selected with probability k / n. You can see that with the proof below the equation:
https://www.facebook.com/photo.php?fbid=677984275648191&l=7cafe5d468
source share