首页 > 代码库 > rand(5) -> rand(7)

rand(5) -> rand(7)

int rand7() //random number from 1 - 7{    int r = 0;    do    {       int a = rand(5) - 1; //uniformly at random from 0 to 4       int b = rand(5) - 1;  //uniformly at random from 0 to 4       r = 5*b + a;  //uniformly at random from 0 to 24    }    while (r >= 21); // in this event, we have to roll again   //postcondition of loop: we have a number uniformly at random between 0 and 20  return r % 7 + 1;     //there are 3 numbers in [0, 20] for each possible return value  //so each has equal probability.      }

 

确保每一步都是distribution uniformly.

rand(5) -> rand(7)