Reputation: 24593
It's a common interview question to generate a larger range of random numbers using a random number generator for a smaller range. For example, given a function rand5
that randomly generates numbers from 0 to 5, create rand7
. There is a SO thread for that.
How do we do the opposite, create rand5
given rand7
?
Upvotes: 0
Views: 555
Reputation: 24219
it is quite simple, in pseudo-code:
do {
r = rand7
} while r>5
return r;
Upvotes: 2