select m objects from n objects randomly

Q: how to select m objects randomly from n objects with euqal possibility?

A: allocate an array of m elements to keep the final result.

    put the first m objects into the array.

  foreach object k numbered from m+1 to n, generate an random rational number a in [0, 1],

     if a <= m/k then

    generate an random integer number in [1, m], say i, remove the i-th element from the final result,

           and put the kth number into it.

//PROVE the answer.

We can use mathematics induction.

The propositonal statement is P(n): the method in the answer can be used to select m objects out of n objects randomly, and each object is picked out with possibility of m/n, (n>=m).

Basis step:

P(m) is true since the answer put the first m elemets into the final result.

induction step:

The induction hypothesis is P(k) is true for all k>=m, then

the (k+1)-th element will be select with possibility of m/(k+1).

Any element kept in the final result before iteration k+1 has a possibility to be removed from the array which is m/(k+1) * (1/m), from the hypothesis, so it will stay in the final result with a possibility of m/k * (1 - m/(k+1) * (1/m)) = m/(k+1).

From the basis step and induction step, we have that P(n) is true for any integer n>=m.

原文地址:https://www.cnblogs.com/Torstan/p/3489757.html