0

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?

Abhijit Sarkar
  • 19,114
  • 16
  • 94
  • 178

1 Answers1

2

it is quite simple, in pseudo-code:

do {
    r = rand7
} while r>5
return r;
Iłya Bursov
  • 21,884
  • 4
  • 31
  • 54