**C/C++ code** the core code has one line only! static unsigned int gi = 0; int rand7() { return (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1; } //call this seed before rand7 //maybe it's not best seed, if yo have any good idea tell me please //and thanks JiminP again, he remind me to do this void srand7() { srand((unsigned int)time(0)); //initialize the seed for rand int i, n = time(0); for (i = 0; i < n % 7; i++) rand7(); } The srand7() is the seed of rand7, must call this function before rand7, just like call srand before rand in C. This is a very good one, because it call rand() only one time, and no loop thing, no expends extra memories. Let me explain it: consider a integer array with size of 5: 1st get one number from 1 2 3 4 5 by rand5 2nd get one number from 2 3 4 5 6 3rd get one number from 3 4 5 6 7 4th get one number from 4 5 6 7 1 5th get one number from 5 6 7 1 2 5th get one number from 6 7 1 2 3 7th get one number from 7 1 2 3 4 So we got the TABLE, each one of 1-7 appears 5 times in it, and has all 35 numbers, so the probability of each number is 5/35=1/7. And next time, 8th get one number from 1 2 3 4 5 9th get one number from 2 3 4 5 6 ...... After enough times, we can get the uniform distribution of 1-7. So, we can allocate a array to restore the five elements of 1-7 by loop-left-shift, and get one number from array each time by rand5. Instead, we can generate the all seven arrays before, and using them circularly. The code is simple also, has many short codes can do this. But, we can using the properties of % operation, so the table 1-7 rows is equivalent with (rand5 + i) % 7, that is : a = rand() % 5 + 1 is rand5 in C language, b = gi++ % 7 generates all permutations in table above, and 0 - 6 replace 1 - 7 c = (a + b) % 7 + 1, generates 1 - 7 uniformly. Finally, we got this code: (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1 But, we can not get 6 and 7 at first call, so we need a seed, some like srand for rand in C/C++, to disarrange the permutation for first formal call. Here is the full code to testing: #include <stdio.h> #include <stdlib.h> #include <time.h> static unsigned int gi = 0; //a = rand() % 5 + 1 is rand5 in C language, //b = gi++ % 7 generates all permutations, //c = (a + b) % 7 + 1, generates 1 - 7 uniformly. //Dont forget call srand7 before rand7 int rand7() { return (((rand() % 5 + 1) + (gi++ % 7)) % 7) + 1; } //call this seed before rand7 //maybe it's not best seed, if yo have any good idea tell me please //and thanks JiminP again, he remind me to do this void srand7() { srand((unsigned int)time(0)); //initialize the seed for rand int i, n = time(0); for (i = 0; i < n % 7; i++) rand7(); } void main(void) { unsigned int result[10] = {0}; int k; srand7() //initialize the rand7 for (k = 0; k < 100000; k++) result[rand7() - 1]++; for (k = 0; k < 7; k++) printf("%d : %.05f\n", k + 1, (float)result[k]/100000); }