如何编写一个函数,该函数使用另一个生成 10000 个值的函数的 2 个值?

How to write a function that uses 2 values of another function which generates 10000 values?

我编写了一个线性同余生成器,可以将 10000 个伪随机值打印到终端。这是模块的一部分,因此它有自己的功能。我现在想编写一个新函数,它采用 2 个随机均匀分布的数字,并在 box muller 方法中使用它们来生成另外 2 个数字。我了解 box muller 部分本身,我只是不明白我如何对其进行编程以仅从之前的 10000 中获取 2 个值?这是我的代码:

module rng
  implicit none

  integer, parameter :: dp = selected_real_kind(15,300)
  real(kind=dp) :: A=100, B= 104001, M = 714025 

contains 

function lcg(seed)
  integer :: lcg
  integer, optional, intent(in) :: seed
  real(kind=dp) :: x = 0

  if(present(seed)) x = seed
  x = mod(A * x + B, M)
  lcg = x
end function

end module


program lcgtest
  use rng
  implicit none
  integer :: N


  do N = 1, 10000
    print *, lcg()
  end do
end program 

谢谢。

您的函数生成 1 个整数,而不是 1000。只需调用它两次,您就会得到两个数字。

do
  a = lcg ()
  b = lcg ()

  !do something with a and b
end do