利用rand7() 产生rand10()(腾讯)

题目1:已知rand7() 可以产生 1~7 的7个数(均匀概率),利用rand7()  产生rand10()   1~10(均匀概率)

int rand10()
{
int temp;
int temp2;
do 
{
temp = rand7();
} while (temp > 5);//temp 1到5
do 
{
temp2 = rand7();
while (temp2 > 2);//temp2 1到2
return temp + (temp2 - 1) * 5;
}

 题目2:给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数。

原文地址:https://www.cnblogs.com/seven7seven/p/3659651.html