生成独立的伪随机数字序列 - fortran 90 [英] Generate independent pseudo-random number sequences - fortran 90

查看:225
本文介绍了生成独立的伪随机数字序列 - fortran 90的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Fortran 90相对比较陌生,尽管已经搜遍了大量的手册,论坛,教程,大学页面和书籍,但我还没有设法使用系统时钟修改F90例程,以便生成随机数在[0,1]中)从一次跑向另一次跑是独立的。

I am relatively new to Fortran 90 and despite having scoured a lot of manuals, forums, tutorials, university pages, and books, I have not managed to modify the F90 routine below using the system clock so that the random numbers generated (in [0,1]) are independent from one run to another.

! 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

对于像我这样的初学者来说,代码并不那么容易理解。
关于如何使用系统时钟正确执行此操作的任何见解/建议/解决方案?

The code is not that easy to fully understand for a beginner like me. Any insights/suggestions/solutions on how to properly do this using the system clock?

推荐答案

初始化,您可以直接复制和使用,在gfortran手册中。请参阅 http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html

An example of such initialization, which you can just copy and use directly, is in the gfortran manual. See http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html

使用许多编译器,只需运行 RANDOM_SEED()而无需参数,并在处理器中设置序列 - 依赖方式使用时钟。然而Gfortran在这种情况下不使用时钟。因此请使用该手册的链接中的子程序。

With many compilers you can just run RANDOM_SEED() without arguments and they set the sequence in the processor-dependent manner using the clock. Gfortran however does not use the clock in this case. Therefore use the subroutine from the link to the manual.

这篇关于生成独立的伪随机数字序列 - fortran 90的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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