本文介绍了位和INT的关系/浮随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚位和RNG的整数或浮点数之间的关系。

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

(通过的任意的我的意思是均匀分布的)

(By random I mean uniformly distributed)

我给出的完美的布尔随机数发生器,并要求我实现一个随机的32位整数发生器(包括负数,零和正数)。我想要做的就是生成一个随机的布尔为每个32位,Concat的在一起是一个随机INT 的。

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.

我是做正确的事?

另外从周围的其他方法,如果我给出的完美的随机32位整数发生器,可我说的每一位都可以看作是在0和1均匀分布的?

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?

怎么样浮动(不只是0和1之间,但全范围浮动)?

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

我可以用同样的方法来生成随机IEEE 745浮点数?

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

推荐答案

是的,你确实是这样做是正确的,使用32得出不同的数字,会给你一个均匀分布的随机变量。

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

说明:每个数字都可以被32 0/1绘制一个独特的组合产生。从相同的组合不产生2号,并且没有数从2的组合产生的 - 的含义,每个数字的概率为1/2 ^ 32 - 如预期

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.

是的,该原理同样适用于这里。有2 ^ 32'方式'来选择32位数字,并同样地previous问题 - 你可以看到,数字独立地均匀地在{0,1}的每比特分布

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.

在区间[0,1]随机均匀分布的浮动可以通过生成randUnsignedInt()/(2 ^ 32-1)。另一种方法是绘制 INT ,只是再跨preT它的广告浮法 - 假设两个使用相同的比特数(基本上是 - 无论是32位的号码,它们只在你的方式跨preT他们...)注意,另一种方法是不在范围内[0,1]。

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

这篇关于位和INT的关系/浮随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:10