rng_t Derived Type

type, public :: rng_t

Random number generator type, which contains the state


Inherited by

type~~rng_t~~InheritedByGraph type~rng_t rng_t type~prng_t prng_t type~prng_t->type~rng_t rngs

Components

Type Visibility Attributes Name Initial
integer(kind=i8), private :: s(2) = [123456789_i8, 987654321_i8]

The rng state (always use your own seed)


Type-Bound Procedures

procedure, public, non_overridable :: circle

  • private function circle(self, radius) result(xy)

    Sample point on a circle with given radius

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self
    real(kind=dp), intent(in) :: radius

    Return Value real(kind=dp), (2)

procedure, public, non_overridable :: int_4

  • private function int_4(self)

    Return 4-byte integer

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value integer(kind=i4)

procedure, public, non_overridable :: int_8

  • private function int_8(self)

    Return 8-byte integer

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value integer(kind=i8)

procedure, public, non_overridable :: jump

  • private subroutine jump(self)

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

procedure, public, non_overridable :: next

  • private function next(self) result(res)

    Interal routine: get the next value (returned as 64 bit signed integer)

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value integer(kind=i8)

procedure, public, non_overridable :: normal

  • private function normal(self)

    Single normal random number

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value real(kind=dp)

procedure, public, non_overridable :: poisson

  • private function poisson(self, lambda) result(rr)

    Return Poisson random variate with rate lambda. Works well for lambda < 30 or so. For lambda >> 1 it can produce wrong results due to roundoff error.

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self
    real(kind=dp), intent(in) :: lambda

    Return Value integer(kind=i4)

procedure, public, non_overridable :: set_seed

  • private subroutine set_seed(self, the_seed)

    Set a seed for the rng

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self
    integer(kind=i8), intent(in) :: the_seed(2)

procedure, public, non_overridable :: sphere

  • private function sphere(self, radius) result(xyz)

    Sample point on a sphere with given radius

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self
    real(kind=dp), intent(in) :: radius

    Return Value real(kind=dp), (3)

procedure, public, non_overridable :: two_normals

  • private function two_normals(self) result(rands)

    Return two normal random variates with mean 0 and variance 1. http://en.wikipedia.org/wiki/Marsaglia_polar_method

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value real(kind=dp), (2)

procedure, public, non_overridable :: unif_01

  • private function unif_01(self)

    Get a uniform [0,1) random real (double precision)

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self

    Return Value real(kind=dp)

procedure, public, non_overridable :: unif_01_vec

  • private subroutine unif_01_vec(self, rr)

    Fill array with uniform random numbers

    Arguments

    Type IntentOptional Attributes Name
    class(rng_t), intent(inout) :: self
    real(kind=dp), intent(out) :: rr(:)

Source Code

  type rng_t
     !> The rng state (always use your own seed)
     integer(i8), private       :: s(2) = [123456789_i8, 987654321_i8]
   contains
     procedure, non_overridable :: set_seed    ! Seed the generator
     procedure, non_overridable :: jump        ! Jump function (see below)
     procedure, non_overridable :: int_4       ! 4-byte random integer
     procedure, non_overridable :: int_8       ! 8-byte random integer
     procedure, non_overridable :: unif_01     ! Uniform (0,1] real
     procedure, non_overridable :: unif_01_vec ! Uniform (0,1] real vector
     procedure, non_overridable :: normal      ! One normal(0,1) number
     procedure, non_overridable :: two_normals ! Two normal(0,1) samples
     procedure, non_overridable :: poisson     ! Sample from Poisson-dist.
     procedure, non_overridable :: circle      ! Sample on a circle
     procedure, non_overridable :: sphere      ! Sample on a sphere
     procedure, non_overridable :: next        ! Internal method
  end type rng_t