Expand a random range from 1–5 to 1–7

前端 未结 30 3310
一个人的身影
一个人的身影 2020-11-22 07:29

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.

  1. What is a simple so
30条回答
  •  暖寄归人
    2020-11-22 08:35

    (I have stolen Adam Rosenfeld's answer and made it run about 7% faster.)

    Assume that rand5() returns one of {0,1,2,3,4} with equal distribution and the goal is return {0,1,2,3,4,5,6} with equal distribution.

    int rand7() {
      i = 5 * rand5() + rand5();
      max = 25;
      //i is uniform among {0 ... max-1}
      while(i < max%7) {
        //i is uniform among {0 ... (max%7 - 1)}
        i *= 5;
        i += rand5(); //i is uniform {0 ... (((max%7)*5) - 1)}
        max %= 7;
        max *= 5; //once again, i is uniform among {0 ... max-1}
      }
      return(i%7);
    }
    

    We're keeping track of the largest value that the loop can make in the variable max. If the reult so far is between max%7 and max-1 then the result will be uniformly distrubuted in that range. If not, we use the remainder, which is random between 0 and max%7-1, and another call to rand() to make a new number and a new max. Then we start again.

    Edit: Expect number of times to call rand5() is x in this equation:

    x =  2     * 21/25
       + 3     *  4/25 * 14/20
       + 4     *  4/25 *  6/20 * 28/30
       + 5     *  4/25 *  6/20 *  2/30 * 7/10
       + 6     *  4/25 *  6/20 *  2/30 * 3/10 * 14/15
       + (6+x) *  4/25 *  6/20 *  2/30 * 3/10 *  1/15
    x = about 2.21 calls to rand5()
    

提交回复
热议问题