Question

I want to figure out the relationship between bits and RNG for int or float.

(By random I mean uniformly distributed)


I am given a perfect boolean random generator, and I am asked to implement a random 32 bits integer generator (including negative, zero and positive). What I want to do is generate a random boolean for each of the 32 bits, and concat them together to be a random int.

Am I doing the right thing?


Also from the other way around, if I am given a perfect random 32 bits integer generator, can I say each bit can be considered as uniformly distributed over 0 and 1?


how about float (not only between 0 and 1, but the full range of float)?

Can I use the same way to generate random IEEE 745 float?

Was it helpful?

Solution

Yes, you are indeed doing it right, using 32 draws of the different numbers, will give you a uniform distributed random variable.

Explanation: Each number can be generated by a unique combination of 32 0/1 draws. No 2 numbers are generated from the same combination, and no number is generated from 2 combinations - meaning, the probability for each number is 1/2^32 - as expected.


Yes, Same principle applies here. There are 2^32 'ways' to choose 32 bits number, and similarly to the previous question - you can see that the numbers are independently uniformly distributed on {0,1} per bit.


A random uniformly distributed float in range [0,1] can be generated by randUnsignedInt()/(2^32-1). An alternative is drawing an int and just re-interpret it ad float - assuming both are using the same number of bits (basically - both are 32 bits number, they only vary in the way you interpret them...) Note that the alternative is NOT in range [0,1].

OTHER TIPS

Yes. This is exactly what random.org does. In this case, the domains are fairly easy to map across -- things like a 6 sided die are harder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top