本文介绍了开放开区间(0,1)有效地产生随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种有效的方式来产生就开开区间(0,1)随机浮点​​数。我现在有上的封闭闭区间生成随机整数的RNG [0,(2 ^ 32)-1]。我已经通过简单地通过我的结果,从整数的RNG乘以创建在区间[0,1)半开浮点RNG 1 /((2 ^ 32)-1),而不是除以(2 ^ 32) -1,因为它是低效率的。

I'm looking for an efficient way to generate random floating-point numbers on the open-open interval (0,1). I currently have an RNG that generates random integers on the closed-closed interval of [0, (2^32)-1]. I've already created a half-open floating point RNG on the interval [0,1) by simply multiplying my result from the integer RNG by 1/((2^32)-1) rather than dividing by (2^32)-1 since it's inefficient.

目前我打算约在区间(0,1)生成号码的方法是用类似下面的条件语句:

The way I'm currently going about generating numbers on the interval (0,1) is with a conditional statement like the one below:

float open_open_flt = (closed_open_flt==0) ? closed_open_flt : FLT_MIN;

不幸的是,这是相当低效的,因为它是控制code,我觉得它引入了一些偏差。

Unfortunately, this is rather inefficient since it is control code and I feel like it introduces some bias.

任何人都可以提出一个替代方案?

Can anybody suggest an alternative?

推荐答案

您已经在那里了。

两个浮点数当前的发电机产生之间的最小距离为1 /(2 ^ 32)。

The smallest distance between two floats your current generator produces is 1/(2^32).

所以,你的发电机efectively生产的 [0.1-1 /(2 ^ 32)]

So, your generator is efectively producing [0,1-1/(2^32)].

1 /(2 ^ 32)大于FLT_MIN更大。

1/(2^32) is greater than FLT_MIN.

因此​​,如果您添加FLT_MIN到您的发电机,

Thus if you add FLT_MIN to your generator,

float open_open_flt = FLT_MIN + closed_open_flt;

你会得到的 [FLT_MIN,1-(1 /(2 ^ 32))+ FLT_MIN] ,它可以作为一个(0,1)发电机。

you'll get [FLT_MIN,1-(1/(2^32))+FLT_MIN], which works as a (0,1) generator.

这篇关于开放开区间(0,1)有效地产生随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:47