4 ms·
Can't you just do this ? int rand7() { return (int)(7.0f * rand5() / 5.0f); }
by csl 18y ago
Can't you just do this ?
int rand7()
{
return (int)(7.0f * rand5() / 5.0f);
}
- csl 18y agoNo -- I get zero hits for 4 and 7 with this approach.
- csl 18y agoOk, just to limit my embarrassment, I've implemented an alternative solution -- though not as good as the others. 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(); } Now we simply build a three bit number: int rand7() { int n = rand2(); n |= rand2() << 1; n |= rand2() << 2; return n; } This gives the proper distribution as well, but it's not branch free, so really nothing new here.