As it has been answered in Adam Rosenfield’s solution a while ago you may try to use an algoritm similar to this one below:
int i;
do
{
i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 and 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1; // result is now uniformly random between 1 and 7
EDIT: Translation to python:
def rand7():
i = 0;
while True:
i = 5 * (rand5() -1) + rand5()
if i > 21:
break
return i % 7 + 1
1
solved Creating a function to generate a random number [closed]