PGI Fortran中的随机数生成器不是那么随机 [英] Random number generator in PGI Fortran not so random

查看:122
本文介绍了PGI Fortran中的随机数生成器不是那么随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码仅生成一个简单的随机数三元组:

The following code just generates a simple triple of random numbers:

program testrand

integer, parameter :: nz = 160, nf = 160, nlt = 90

real :: tmpidx(3)
integer :: idxarr(3), idx1, idx2, idx3, seed_size, ticks
integer, allocatable :: seed(:)

call random_seed(size=seed_size)
allocate(seed(seed_size))

call system_clock(count=ticks)
seed = ticks+37*(/(i-1, i=1,seed_size)/)
call random_seed(put=seed)

deallocate(seed)

call random_number(tmpidx)
idxarr = tmpidx * (/nz, nf, nlt/)
idx1 = max(1,idxarr(1))
idx2 = max(1,idxarr(2))
idx3 = max(1,idxarr(3))
print *,idx1, idx2, idx3

end program

我用gfortran编译并运行了几次,然后得到:

I compile this with gfortran and run a few times and I get:

> gfortran testrand.f90
> ./a.out
          74          98          86
> ./a.out
         113           3          10
> ./a.out
          44         104          27

看起来很随意.现在,我使用PGI Fortran编译并运行几次:

Looks pretty random. Now I compile with PGI Fortran and run a few times:

> pgf90 testrand.f90
> ./a.out
            1            1            1
> ./a.out
            1            1            1
> ./a.out
            1            1            1

当然,没有办法完全确定,但是我怀疑这不是随机的. :)有人知道这是怎么回事吗?任何人都知道使用PGI Fortran获取随机数的正确方法吗?

Of course, there's no way to be completely sure, but I suspect this is not random. :) Anyone know what is going on here? Anyone know the right way to get random numbers with PGI Fortran?

推荐答案

以某种方式,PGI不像GNU编译器那样实现system_clock.我不知道为什么,最近我是通过做类似你的事情来发现它的.

Somehow, PGI does not implement system_clock as in GNU compilers. I do not know why, I found it recently by doing similar stuff like you.

要了解我在说什么,只需在调用system_clock之后打印ticks.很有可能您总是使用PGI来获取0,而使用GNU编译器则可以得到不同的数字.为了解决您的问题,您可以修改下面的代码.它是您可以在 GNU fortran网站

To see what I am talking about, just print ticks after calling system_clock. Chances are that you get 0 all the time with PGI and varying numbers with GNU compilers. To solve your problem, you can adapt the code bellow. It is a slightly modified version of a code that you can get at GNU fortran web site

program testrand
use iso_fortran_env, only: int64

    integer, parameter :: nz = 160, nf = 160, nlt = 90
    real :: tmpidx(3)
    integer :: idxarr(3), idx1, idx2, idx3, seed_size, ticks
    integer, allocatable :: seed(:)

    call random_seed(size=seed_size)
    allocate(seed(seed_size))

    ! call system_clock(count=ticks)
    ! seed = ticks+37*(/(i-1, i=1,seed_size)/)
    ! call random_seed(put=seed)
    ! 
    ! deallocate(seed)

    call init_random_seed()

    call random_number(tmpidx)
    idxarr = tmpidx * (/nz, nf, nlt/)
    idx1 = max(1,idxarr(1))
    idx2 = max(1,idxarr(2))
    idx3 = max(1,idxarr(3))
    print *,idx1, idx2, idx3

contains
    !
    subroutine init_random_seed()
        implicit none
        integer, allocatable :: seed(:)
        integer :: i, n, istat, dt(8), pid
        integer(int64) :: t

        integer, parameter :: un=703

        call random_seed(size = n)
        allocate(seed(n))
        ! First try if the OS provides a random number generator
        open(unit=un, file="/dev/urandom", access="stream", &
            form="unformatted", action="read", status="old", iostat=istat)
        if (istat == 0) then
            read(un) seed
            close(un)
        else
            ! The PID is
            ! useful in case one launches multiple instances of the same
            ! program in parallel.
            call system_clock(t)
            if (t == 0) then
                call date_and_time(values=dt)
                t = (dt(1) - 1970) * 365_int64 * 24 * 60 * 60 * 1000 &
                + dt(2) * 31_int64 * 24 * 60 * 60 * 1000 &
                + dt(3) * 24_int64 * 60 * 60 * 1000 &
                + dt(5) * 60 * 60 * 1000 &
                + dt(6) * 60 * 1000 + dt(7) * 1000 &
                + dt(8)
            end if
            pid = getpid()
            t = ieor( t, int(pid, kind(t)) )
            do i = 1, n
                seed(i) = lcg(t)
            end do
        end if
        call random_seed(put=seed)
        !print*, "optimal seed = ", seed
    end subroutine init_random_seed
    !
    function lcg(s)
        integer :: lcg
        integer(int64), intent(in out) :: s
        if (s == 0) then
            s = 104729
        else
            s = mod(s, 4294967296_int64)
        end if
        s = mod(s * 279470273_int64, 4294967291_int64)
        lcg = int(mod(s, int(huge(0), 8)), kind(0))
    end function lcg
    !
    !this option is especially used for pgf90 to provide a getpid() function
    !> @brief Returns the process ID of the current process
    !! @todo write the actual code, for now returns a fixed value
    !<
    function getpid()result(pid)
        integer pid
        pid = 53 !just a prime number, no special meaning
    end function getpid
end program

这篇关于PGI Fortran中的随机数生成器不是那么随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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