First convert rand5() to rand2(). The LSB from rand5() has a uniform distribution for the integers 0--3:
int rand2() { int n = rand5(); return n!=4 ? n & 1 : rand2(); }
int rand7() { int n = rand2(); n |= rand2() << 1; n |= rand2() << 2; return n; }
First convert rand5() to rand2(). The LSB from rand5() has a uniform distribution for the integers 0--3:
Now we simply build a three bit number: This gives the proper distribution as well, but it's not branch free, so really nothing new here.