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
  • 21,927
  • 20
  • 110
  • 219

1 Answers1

2

it is quite simple, in pseudo-code:

do {
    r = rand7
} while r>5
return r;
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57