2

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

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

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

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.

Some google searches haven't turned up any explanation, so I'm wondering if anyone here can explain why these results are so different?

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?

  • This isn't a full answer to your question. You should consult your compiler documentation for details of the PRNG. There is every possibility that a seed you choose without making effort is a terrible one. Some generators have areas of very low entropy with poor seeds. – francescalus Mar 08 '18 at 00:32
  • I suggest deleting the final part: asking for external resources is considered off-topic. – francescalus Mar 08 '18 at 00:33
  • 1
    You might find some helpful material at a related SO question: [Random number generator in PGI Fortran not so random](https://stackoverflow.com/q/34797938/7038689). – Matt P Mar 08 '18 at 05:08
  • @MattP, that other question seems more about issues with how each compiler handles system_clock. – Walter Hannah Mar 08 '18 at 16:53
  • 1
    @francescalus, I looked at some documentation but it didn't mention anything about this issue. – Walter Hannah Mar 08 '18 at 16:53
  • That's true, but the subroutine init_random_seed may be worth trying. – Matt P Mar 08 '18 at 17:02

1 Answers1

2

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.

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.

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

  • Sorry for the redundant topic, I tried searching for this, but nothing came up (which surprised me). I guess I was searching for the wrong thing. – Walter Hannah Mar 08 '18 at 16:55