I have a project which uses php’s mt_rand() to generate different random integers but I have recently gained access to a stream of real random bits. I am having trouble figuring out how to create a function similar to mt_rand(), where I can get a random integer between two values, from my stream of bits. How can I achieve this?
I have a project which uses php’s mt_rand() to generate different random integers but
Share
I have come up with a method but not sure if its the most efficient way in terms of bits used (code is untested, just demonstrating theory):
As you can see, there will be a chance the function is repeated, which means more bits will be used, thats why I’m not sure if its the most efficent way, in terms of bits used, to do it.
Pr(repeat) = (2^bitsNeeded - range) / (2^bitsNeeded)Therefore, 0 < Pr(repeat) < 0.5So in the worst case that the chance of having to repeat is almost 0.5, it starts to get quite unlikely that it would need to be repeated more than 10 or so times, with the average being just under 2 times. Obviously at lower chances of having to repeat , these numbers get lower.