IMHO: When you need a random number you want it to be truly random.
Predictable is not always a bad thing, but you want random distribution.
So "(rand5() + 5*rand5())%7 + 1" would not be a valid answer.
Quote:
the question is how much time you have to find this answer. if you have a whole day to experiment and write test programs it wouldn't be that hard.
Normally they ask these questions during an on-site interview, but this could be a take home test to help weed out candidates.
Even with a whole day, most people wouldn't get it right.
Quote:
as i said, it seems impossible to acheive both equal distribution and no predictability without at least one branch.
Nope. It is possible but (as far as I can see) not practical.
One method involves generating the numbers "offline" and just using a lookup table. Another method requires very large numbers.