If given a function that generates a random number from 1 to 5, how do you use this function to generate a random number from 1 to 7 with the same probability? (ie. function a has probability 1/5 per number, function b would have probability 1/7).
int sum = 0;
int times = 5;
while(times>0){
times--;
sum += rand(5);
}
// 5 ~ 25 = 3 *7
return (sum - 2) / 3;
时间: 2024-11-05 20:34:10