简体   繁体   English

在 fortran90/95 或 Python 中的两个大整数之间生成均匀随机数

[英]Generating uniform random numbers between two large integers in fortran90/95 or Python

I want to generate some uniform random numbers between [2E16, 5E20] in fortran90/95 or Python.我想在 fortran90/95 或 Python 中的 [2E16, 5E20] 之间生成一些统一的随机数。 I know that if "R" is a random number between zero and one (0<R<1), we can transform R to [A, B] as:我知道如果“R”是零和一之间的随机数(0<R<1),我们可以将 R 转换为 [A, B] 为:

R = (ba)*r + a. R = (ba)*r + a。

But when I use this algorithm to generate some random numbers between [2E16, 5E20], most of generated random numbers are very close to 5E20.但是当我使用这个算法在[2E16, 5E20]之间生成一些随机数时,大多数生成的随机数都非常接近5E20。 I know why this is occurred, it is because of that, the term BA is approximately equal to B (because B is so bigger than A), and most of the generated random numbers are placed within the magnitude of B or B/10 (I mean it was very close to B).我知道为什么会发生这种情况,正因为如此,术语 BA 大约等于 B(因为 B 比 A 大得多),并且大多数生成的随机数都放在 B 或 B/10 的大小范围内(我的意思是它非常接近B)。

How can I solve this problem?我怎么解决这个问题?

One solution that I tried to apply is that, I split the [2E16, 5E20] interval into smaller intervals, such as:我尝试应用的一种解决方案是,我将 [2E16, 5E20] 区间拆分为更小的区间,例如:

[2e16, 2e17], [2e17, 2e18], [2e18, 2e19], [2e19, 2e20], [2e20, 5e20]. [2e16, 2e17], [2e17, 2e18], [2e18, 2e19], [2e19, 2e20], [2e20, 5e20]。

And I generated some random numbers between each of the above intervals.我在上述每个间隔之间生成了一些随机数。

Is there any other solutions?还有其他解决方案吗? Is my solution correct?我的解决方案正确吗?

This is less of a programming problem, and more of a maths problem.这不是一个编程问题,而是一个数学问题。

You need to think about which distribution you are drawing your random numbers from.您需要考虑从哪个分布中提取随机数。 Any uniform distribution which spans several orders of magnitude will always produce more numbers with larger exponents than with smaller exponents.任何跨越几个数量级的 均匀分布总是会产生更多具有较大指数的数字而不是具有较小指数的数字。

If you want a distribution which produces the same number of numbers with each exponent, you should consider the log uniform distribution , which can be sampled using eg scipy.stats.loguniform .如果您想要一个与每个指数产生相同数量的数字的分布,您应该考虑对数均匀分布,可以使用例如scipy.stats.loguniform对其进行采样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM