I was reading a question about generating rand7() from rand5(), and I still don't quite seem to understand it. The suggested solution is shown here:
        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
I understand the approach, but I don't understand why the range has to be from 1 to 21. My solution would be this:
        int i;
        do
        {     
          i = (rand5()-1) + rand5();  // i is now uniformly random between 1 and 9
        } while(i > 7); 
        // i is now uniformly random between 0 and 6
        return i+1;
I haven't been able to convince myself the approach outlined above doesn't work. Can you guys give me an example of numbers that appears more than others, making my approach non-uniform? Why is the multiplier of 5 needed?
 
     
     
    