The relationship between bits and int/float random number generator

混江龙づ霸主 提交于 2020-01-06 08:30:45

问题


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?


回答1:


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].




回答2:


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.



来源:https://stackoverflow.com/questions/22939125/the-relationship-between-bits-and-int-float-random-number-generator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!