rng_normal Function

public function rng_normal(self) result(n)

Generate a normally-distributed random number with mean 0 and standard deviation 1

Arguments

TypeIntentOptionalAttributesName
type(rng_type), intent(inout) :: self

Random Number Generator

Return Value real(kind=Float64)

Normal random deviate


Calls

proc~~rng_normal~~CallsGraph proc~rng_normal rng_normal proc~rng_uniform rng_uniform proc~rng_normal->proc~rng_uniform

Contents

Source Code


Source Code

function rng_normal(self) result(n)
    !+ Generate a normally-distributed random number with mean 0 and standard deviation 1
    type(rng_type), intent(inout) :: self
        !+ Random Number Generator
    real(Float64)                 :: n
        !+ Normal random deviate

    real(Float64), parameter :: s = 0.449871d0
    real(Float64), parameter :: t = 0.386595d0
    real(Float64), parameter :: a = 0.196000d0
    real(Float64), parameter :: b = 0.254720d0
    real(Float64), parameter :: r1 = 0.27597d0
    real(Float64), parameter :: r2 = 0.27846d0
    real(Float64) :: u, v, x, y, q

    do
        u = rng_uniform(self)
        v = rng_uniform(self)
        v = 1.7156d0 * (v - 0.5d0)

        x = u - s
        y = abs(v) + t
        q = x**2 + y*(a*y - b*x)

        if (q.lt.r1) exit
        if (q.gt.r2) cycle
        if((v**2).lt.(-4.0*log(u)*u**2)) exit
    enddo

    n = v/u

end function rng_normal