Abhijit Sarkar
Abhijit Sarkar

Reputation: 24593

Compute rand5 given rand7

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

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24219

it is quite simple, in pseudo-code:

do {
    r = rand7
} while r>5
return r;

Upvotes: 2

Related Questions