This question is likely if you are dealing with quantum randomizers, etc. It’s irrelevant if there is an appropriate function in the language.
You are given a function that generates 0 and 1 with equal probability. Write a function that uses the above function to generate any n(1<=n<=1000) so that probability of producing any number between 1 to 1000 is equal and is 1/1000.
The solution is simple and fast using bit shifting (which gives performance, *2 could be used but that will raise flags with many interviewers)
Often the ZeroOne would be put internal to the Random1to1000(). If this is done, then to ask for it to be moved to an external function ZeroOne() will usually reveal whether there is understanding of Random issues (i.e. initializing Random inside of ZeroOne will not only result in poorer performance, but will also loose randomness!).
The testing is simple:
The results allows spot testing:
Often people will assume that zero(0) will not occur from the code (to see a lot of code that allows a zero to be returned, see this).