random_number()在GNU和PGI fortran编译器之间提供了截然不同的行为 [英] random_number() gives vastly different behavior between GNU and PGI fortran compilers
问题描述
这是我用来了解fortran固有统一随机数生成器行为的一个简单的fortran程序.
Here's a simple fortran program I was using to understand the behavior of the fortran intrinsic uniform random number generator.
program test_prog
implicit none
integer, allocatable :: seed(:)
real(8), dimension(2) :: unif_rand
integer :: nseed ! minimum number of random seed value
integer :: i,n
call random_seed( size=nseed )
nseed = 100
allocate( seed(nseed) )
write(*,*) "nseed: ",nseed
do n = 1,5
seed(:) = n**10
call random_seed( put=seed )
call random_number(harvest=unif_rand)
write(*,1000) seed(nseed),unif_rand(1),unif_rand(2)
write(*,*) ""
1000 format(i12," ",f12.8," ",f12.8)
enddo
end program test_prog
当我使用gfortran进行编译时,会得到明智的结果:
When I compile with gfortran I get sensible results:
1 0.76322100 0.72975598
1024 0.30901699 0.80380552
59049 0.05916934 0.69849271
1048576 0.59972035 0.71558547
9765625 0.79167428 0.37621382
但是当我用pgf90编译时,会得到非常不同的结果:
But when I compile with pgf90 I get very different results:
1 0.00000024 0.00000024
1024 0.00024414 0.00024414
59049 0.01407838 0.01407838
1048576 0.25000003 0.25000003
9765625 0.32830648 0.32830648
在较小的种子值的情况下,PGI结果始终非常接近于零,因此,似乎PGI编译器会做一些事情来使随机值按种子值进行缩放.这对于我当前的项目是非常成问题的,因为我需要它为不同的编译器提供一致的结果.
With small seed values the PGI results are always very close to zero, so it seems the PGI compiler does something to make the random values such that they are scaled by the seed value. This is very problematic for my current project because I need it to give consistent results for different compilers.
一些Google搜索没有给出任何解释,所以我想知道这里是否有人可以解释为什么这些结果如此不同?
Some google searches haven't turned up any explanation, so I'm wondering if anyone here can explain why these results are so different?
还是没有人知道使PGI编译器结果更符合GNU编译器结果的窍门吗?
Or does anyone know of a trick to make the PGI compiler results more in line with the GNU compiler results?
还是没有人知道一些可以在网上获得的体面随机数生成器的代码,可以代替内在例程来实现?
Or does anyone know of some code for a decent random number generator available online that I could implement as an alternative to the intrinsic routines?
推荐答案
首先,如之前在此处多次讨论的那样,Fortran标准对固有随机数生成器的质量没有任何要求或保证.必须使用外部库来保证质量.
Firstly, as discussed here several times before, the Fortran standard gives NO requirement or guarantee about the quality of the intrinsic random number generator. One has to use external libraries to have any quality guarantee.
第二,PRNG中的 seed
用于提供初始熵的来源.不同的生成器能够成功地处理不同的种子,但是在较差的生成器中,您还需要种子中的位也相对随机"地分布.种子位的一致性太强,结果可能会很糟糕.
Secondly, the seed
in a PRNG is there to give the source of initial entropy. Different generators are able to cope with different seeds with various success but in poor generators you need the bits in the seed to also to be distributed relatively "randomly". Too much coherence of the seed bits and the results may be terrible.
关于要使用的外部库-建议不合时宜,但是建议很多,甚至您还会找到比较各个方法的质量的文章.在这个站点上,在类似问题下的评论或答案中已经给出了许多建议.只需搜索随机和 fortran .
As to which external library to use - recommendations are off topic, but there are plenty of them and you will even find articles with comparisons of the quality of individual methods. Many recommendations were already given on this very site in the comments or answers under similar questions. Just search for random and fortran.
这篇关于random_number()在GNU和PGI fortran编译器之间提供了截然不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!