mod_random.f90 Source File


This file depends on

sourcefile~~mod_random.f90~~EfferentGraph sourcefile~mod_random.f90 mod_random.f90 sourcefile~mod_kinds.f90 mod_kinds.f90 sourcefile~mod_random.f90->sourcefile~mod_kinds.f90

Files dependent on this one

sourcefile~~mod_random.f90~~AfferentGraph sourcefile~mod_random.f90 mod_random.f90 sourcefile~mod_layer.f90 mod_layer.f90 sourcefile~mod_layer.f90->sourcefile~mod_random.f90 sourcefile~mod_network.f90 mod_network.f90 sourcefile~mod_network.f90->sourcefile~mod_layer.f90

Contents

Source Code


Source Code

module mod_random

  !! Provides a random number generator with
  !! normal distribution, centered on zero.

  use mod_kinds, only: ik, rk

  implicit none

  private
  public :: randn

  real(rk), parameter :: pi = 4 * atan(1._rk)

  interface randn
    module procedure :: randn1d, randn2d
  end interface randn

contains

  function randn1d(n) result(r)
    !! Generates n random numbers with a normal distribution.
    integer(ik), intent(in) :: n
    real(rk) :: r(n), r2(n)
    call random_number(r)
    call random_number(r2)
    r = sqrt(-2 * log(r)) * cos(2 * pi * r2)
  end function randn1d

  function randn2d(m, n) result(r)
    !! Generates m x n random numbers with a normal distribution.
    integer(ik), intent(in) :: m, n
    real(rk) :: r(m, n), r2(m, n)
    call random_number(r)
    call random_number(r2)
    r = sqrt(-2 * log(r)) * cos(2 * pi * r2)
  end function randn2d

end module mod_random