PGI Fortran 中的随机数生成器不是那么随机

标签 random fortran fortran90 gfortran pgi

下面的代码只是生成了一个简单的随机数三元组:

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 编译它并运行几次,我得到:

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

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

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

当然,没有办法完全确定,但我怀疑这不是随机的。 :) 有人知道这里发生了什么吗?有人知道使用 PGI Fortran 获取随机数的正确方法吗?

最佳答案

不知何故,PGI 不像 GNU 编译器那样实现 system_clock。我不知道为什么,我最近通过像你一样做类似的事情发现了它。

要了解我在说什么,只需在调用 system_clock 后打印 ticks。使用 PGI 时,您可能总是得到 0,而使用 GNU 编译器时,可能会得到不同的数字。要解决您的问题,您可以修改以下代码。它是代码的略微修改版本,您可以在 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 中的随机数生成器不是那么随机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797938/

相关文章:

javascript - React 只理解随机键?

python - 如何在python中生成但排除某个数字整数中的某个数字

file-io - 打开第 5 或 6 单元上的文件

fortran - Fortran 中具有可分配数组和重载运算符的派生类型

arrays - 声明数组的不同语法 : with and without the dimension statement

language-agnostic - 伪随机数发生器

javascript - 是否可以使用 Array.prototype.reduce 创建数组的线性随机选择?

c - 本身具有依赖性的静态库上的 AC_SEARCH_LIBS?

fortran - 在 Fortran 中将 `implicit none` 放在哪里

Fortran - 编译错误 - 必须具有显式类型