t_random Derived Type

type, public :: t_random

Derived type to hold a random number generator, which does not interfere with the global RANDOM_NUMBER() routine of fortran.

Using t_random can be useful when coupling software which depend on specifically seeded random number generator for internal use, and should not clash with another.


Components

Type Visibility Attributes Name Initial
integer, public :: zseed

value of zseed at which this instance of t_random is initialized.


Constructor

public interface t_random

  • private function t_random_initialize(zseed) result(self)

    Create the t_random object with a seed value zseed. If zseed=0, a new seed is generated based on system_clock values.

    Call as:

     type( t_random ), pointer :: rnd
     integer :: zseed
    
     ! with specified value for zseed:
     rnd => t_random( zseed )
    
     ! or without zseed (this is equivalent to zseed=0)
     rnd => t_random()
    

    Arguments

    Type IntentOptional Attributes Name
    integer, intent(in), optional :: zseed

    if not present, new seed is generated

    Return Value type(t_random), pointer


Finalization Procedures

final :: t_random_destroy

  • private subroutine t_random_destroy(self)

    Arguments

    Type IntentOptional Attributes Name
    type(t_random), intent(inout) :: self

Type-Bound Procedures

procedure, public :: reseed => t_random_reseed

  • private subroutine t_random_reseed(self, zseed)

    re-seed the t_random object with a specific value of zseed.

    initialize random number generator, modified from: https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gfortran/RANDOM_005fSEED.html

    Seed the random number generator with sequence generated from zseed. If on input zseed = 0 then a new, repeatable seed sequence is generated. On output, zseed has value of actual seed used (single value), which can be used to reproduce the actual sequence of seed elements.

    Arguments

    Type IntentOptional Attributes Name
    class(t_random), intent(inout) :: self
    integer, intent(inout) :: zseed

procedure, public :: random_number => t_random_number

  • private subroutine t_random_number(self, z)

    Obtain a random real number z, on range [0:1].

    Arguments

    Type IntentOptional Attributes Name
    class(t_random), intent(inout) :: self
    real, intent(out) :: z(..)

    random[0:1], up to rank-3