zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Linux系统下DedeCMS安全设置详细教程
    乐淘网CEO毕胜:中国不具备购买玩具的文化
    生意
    Sql Server Cpu 100% 的常见原因及优化
    认准了就去做
    雷军谈互联网七字诀:天下武功唯快不破
    电商需要与实体结合才可行
    Xml Schema命名空间
    高效的MySQL分页
    正则
  • 原文地址:https://www.cnblogs.com/Torstan/p/3489757.html
Copyright © 2011-2022 走看看