我对Fortran 90相对较新,尽管已经搜索过很多手册,论坛,教程,大学页面和书籍,但我还是没有设法使用系统时钟修改下面的F90例程,以便生成随机数(在[0,1]中是独立于一次运行。
! Simple random number generator in the range [0,1]
! ranset_(iseed) initializes with iseed
! ranf_() returns next random number
real function ranf_()
implicit none
real rand_
! ranf_ = rand_(0)
call random_number(ranf_)
return
end
subroutine ranset_(iseed)
implicit none
real rand_,ranf_
integer iseed, i, m, nsteps
! i = rand_(1) ! reinitialize (reset)
nsteps = iseed*10000
do i = 1,nsteps
m = ranf_()
! m = rand_(0)
end do
return
end
real function rand_(iseed)
implicit none
integer iseed
integer ia1, ia0, ia1ma0, ic, ix1, ix0, iy0, iy1
save ia1, ia0, ia1ma0, ic, ix1, ix0
data ix1, ix0, ia1, ia0, ia1ma0, ic/0,0,1536,1029,507,1731/
if (iseed.ne.0) then
ia1 = 1536
ia0 = 1029
ia1ma0 = 507
ic = 1731
ix1 = 0
ix0 = 0
rand_ = 0
else
iy0 = ia0*ix0
iy1 = ia1*ix1 + ia1ma0*(ix0-ix1) + iy0
iy0 = iy0 + ic
ix0 = mod (iy0, 2048)
iy1 = iy1 + (iy0-ix0)/2048
ix1 = mod (iy1, 2048)
rand_ = ix1*2048 + ix0
rand_ = rand_ / 4194304.
end if
return
end
对于像我这样的初学者来说,代码并不容易完全理解。 有关如何使用系统时钟正确执行此操作的任何见解/建议/解决方案?
答案 0 :(得分:1)
这种初始化的一个例子,你可以直接复制和使用,在gfortran手册中。见http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
对于许多编译器,您可以在不带参数的情况下运行[y,pmes] = goldbach_partition(n);
,并使用时钟以与处理器相关的方式设置序列。然而,Gfortran在这种情况下不使用时钟。因此,请使用链接中的子程序。