使用WELL512获取一定范围内的随机实数

发布于 2024-11-25 07:59:26 字数 267 浏览 2 评论 0原文

我正在使用本文中描述的WELL512伪随机数生成器函数。该函数返回一个随机unsigned long值。

如何使用此返回值生成特定范围内的随机实数 - 例如 340.92491 和 859812.53198(含)之间的浮点数。

C rand() 函数的文档似乎警告不要使用 mod。

I'm using the WELL512 pseudorandom number generator function described in this paper. The function returns a random unsigned long value.

How do I use this return value to produce a random real number within a certain range - like a float between 340.92491 and 859812.53198 inclusive.

The documentation for the C rand() function seems to warn against using mod.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪是无色的血 2024-12-02 07:59:26

好吧,从数学上讲,它只是:(

min_value + (max_value - min_value) * (my_random() / (long double)ULONG_MAX)

假设 my_random() 返回 0 到 ULONG_MAX 之间均匀分布的数字)

但是,具体取决于 min_valuemax_value>ULONG_MAX,某些浮点数几乎肯定比其他浮点数更有可能出现。

每个可能的随机 unsigned long 通过此公式映射到浮点数。但由于 min_valuemax_value 之间的不同浮点数几乎肯定不完全是 ULONG_MAX,因此一些无符号长整型数将映射到相同的浮点数点数或某些浮点数将没有无符号长映射到它们或两者。

我认为解决这个问题以使结果真正统一是很重要的。也许有人比我更擅长引用论文。

[编辑]

或者查看此问题的答案:

生成基于随机位流的随机浮点值

这个答案取决于 IEEE double 表示的内部结构。我也不确定我是否完全理解它是如何工作的。

[编辑2]

好的,现在我明白它是如何工作的了。这个想法是在最小值和最大值之间选择一个随机浮点表示,然后以与其指数表示的规模成反比的概率将其丢弃。因为对于均匀分布,(例如)1/2 和 1 之间的数字的可能性需要是 1 和 2 之间的数字的一半,但这些范围内的浮点表示形式的数量是相同的。

我认为您可以通过首先选择对数刻度上的指数(例如,通过在随机选择的整数上使用 ffs )然后随机选择尾数来提高该代码的效率。嗯...

Well, mathematically it's just:

min_value + (max_value - min_value) * (my_random() / (long double)ULONG_MAX)

(Assuming my_random() returns a uniformly distributed number between 0 and ULONG_MAX)

However, depending on the exact values of min_value, max_value, and ULONG_MAX, some floating point numbers will almost certainly be more likely than others.

Each possible random unsigned long maps to a float by this formula. But since the number of distinct floating point numbers between min_value and max_value is almost certainly not exactly ULONG_MAX, some unsigned longs will map to the same floating point number or some floating point numbers will have no unsigned long map to them or both.

Fixing this to make the result truly uniform is... non-trivial, I think. Maybe someone better read than I can cite a paper.

[edit]

Or see the answer to this question:

Generating random floating-point values based on random bit stream

That answer depends on the internals of the IEEE double representation. I am also not sure I fully understand how it works.

[edit 2]

OK now I understand how it works. The idea is to pick a random floating point representation between the min and the max, and then to throw it out with probability inversely proportional to its scale as represented by its exponent. Because for a uniform distribution, numbers between (say) 1/2 and 1 need to be half as likely as those between 1 and 2, but the number of floating point representations in those ranges is the same.

I think you could make that code more efficient by first picking the exponent on a logarithmic scale -- say, by using ffs on a randomly-chosen integer -- and then picking the mantissa at random. Hm...

绿萝 2024-12-02 07:59:26

是否可以将实数转换为unsigned long?如果这很容易做到,我认为 WELL512 有效。祝你好运。

Is it possible to convert the real number into unsigned long? If that is easily done, I think WELL512 works. Good luck.

仅此而已 2024-12-02 07:59:26

使用浮点数表示均匀分布的实数是完全不可能的——在你的范围内有无数个实数,但只有有限个浮点数。

更糟糕的是,如果您的范围跨越浮点指数边界,则范围内可表示为浮点数的有限数量的实数甚至可能不是均匀分布的。

It is quite simply impossible to represent uniformly distributed real numbers using floating point numbers - there are an uncountably infinite number of real numbers in your range, but only a finite number of floating point numbers.

What is worse, the finite number of reals within your range that are representable as floating point numbers may not even be evenly distributed, if your range crosses a floating point exponent boundary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文