randomNumber_mod sourceΒΆ

 1module randomNumber_mod
 2  ! MODULE randomNumber_mod (prefix='rng' category='8. Low-level utilities and constants')
 3  !
 4  !:Purpose: A Gaussian random number generator (RNG) module. The actual calculation
 5  !          is performed by functions in an external library.
 6  !
 7  use ISO_C_BINDING
 8  implicit none
 9
10  include 'randomfunctions.inc'
11
12  save
13  private
14
15  ! public procedures
16  public :: rng_setup, rng_gaussian, rng_uniform
17
18  logical :: initialized = .false.
19
20  type(RANDOM_STREAM) :: randomStream
21
22contains
23
24  !--------------------------------------------------------------------------
25  ! rng_Setup
26  !--------------------------------------------------------------------------
27  subroutine rng_setup(seed)
28    !
29    !:Purpose: Initialize the random number generator with a supplied seed.
30    !
31    implicit none
32
33    ! Arguments:
34    integer, intent(in)   :: seed
35
36    ! Locals:
37    integer, dimension(1) :: seeds
38    type(RANDOM_STREAM) :: null_stream
39
40    if (initialized) then
41       write(*,*) 'rng_setup: WARNING: you are re-initializing the module!!!'
42    end if
43
44    seeds(1) = seed
45
46    null_stream = RANDOM_STREAM(C_NULL_PTR)
47
48    ! 'seeds' is an array of dimension 1
49    call Ran_R250_new_stream(randomStream, null_stream, seeds, size(seeds))
50    call RanSetSeed_gaussian_stream(randomStream, seeds, size(seeds))
51
52    initialized = .true.
53
54    write(*,*) 'rng_setup: done using seed = ', seed
55 
56  end subroutine rng_setup
57  
58  !--------------------------------------------------------------------------
59  ! rng_Gaussian
60  !--------------------------------------------------------------------------
61  function rng_gaussian() result(randomNumberGaussian)
62    !
63    !:Purpose: Returns a normally distributed deviate
64    !          with zero mean and unit variance
65    !
66    implicit none
67
68    ! Result:
69    real(8) :: randomNumberGaussian
70
71    randomNumberGaussian = DRan_gaussian_stream(randomStream)
72  end function rng_gaussian
73  
74  !--------------------------------------------------------------------------
75  ! random
76  !--------------------------------------------------------------------------
77  function rng_uniform() result(randomNumberUniform)
78    !
79    !:Purpose: Returns a random deviate between 0.0 and 1.0.
80    !
81    implicit none
82
83    ! Result:
84    real(8) :: randomNumberUniform
85    
86    randomNumberUniform = DRan_generic_stream(randomStream)
87    
88  end function rng_uniform
89  
90end module randomNumber_mod