获得在使用WELL512一定范围内的随机实数 [英] Getting a random real number in a certain range using WELL512

查看:225
本文介绍了获得在使用WELL512一定范围内的随机实数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的本文。该函数返回一个随机的无符号长值。

我如何使用这个返回值在一定范围内产生一个随机实数 - 就像340.92491和859812.53198之间的浮动包容

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


解决方案

好了,数学它只是:

  MIN_VALUE +(MAX_VALUE  -  MIN_VALUE)*(my_random()/(长双)ULONG_MAX)

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

不过,根据 MIN_VALUE MAX_VALUE ULONG_MAX的精确值,某些浮点数字几乎肯定会比其他人更容易。

每个可能的随机unsigned long类型映射到一个浮动通过这个公式。但是,由于不同的浮点数之间的数 MIN_VALUE MAX_VALUE 几乎可以肯定是不完全 ULONG_MAX ,一些未签名多头将映射到相同的浮点数或者一些浮点数将没有无符号长映射到它们或两者

修复此做出真正的结果是一致的...不平凡的,我想。也许有人更好地阅读,比我可以举一个文件。

还是看这个问题的答案:

<一个href=\"http://stackoverflow.com/questions/5015133/generating-random-floating-point-values-based-on-random-bit-stream\">Generating基于随机比特流随机浮点值

这答案取决于IEEE 双击的内部重新presentation。我也不能肯定我完全明白它是如何工作的。

OK,现在我明白它是如何工作的。我们的想法是要挑最小值和最大值之间的随机浮点的再presentation 的,然后用概率成反比,其规模作为其指数psented重新$ P $把它扔出去。因为对于一个均匀分布的,之间的数(比如)1/2和1需要一半的可能性是1和2之间,但是浮点重presentations在这些范围是相同的数目。

我想你可以作出这样的code。通过第一选择对数刻度的指数更有效 - 比如,通过使用一个随机选择的整数 FFS - 然后采摘尾数随意。嗯...

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.

解决方案

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

这篇关于获得在使用WELL512一定范围内的随机实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆