forked from reedmaxwell/EcoSLIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng.f90
47 lines (36 loc) · 1.15 KB
/
rng.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module rng
implicit none
private
public :: rng_t, rng_seed, rng_uniform
! Dimension of the state
integer, parameter :: ns = 4
! Default seed vector
integer, parameter, dimension(ns) :: default_seed &
= (/ 521288629, 362436069, 16163801, 1131199299 /)
! A data type for storing the state of the RNG
type :: rng_t
integer, dimension(ns) :: state = default_seed
end type rng_t
contains
! Seeds the RNG using a single integer and a default seed vector.
subroutine rng_seed(self, seed)
type(rng_t), intent(inout) :: self
integer, intent(in) :: seed
self%state(1) = seed
self%state(2:ns) = default_seed(2:ns)
end subroutine rng_seed
! Draws a uniform real number on [0,1].
function rng_uniform(self) result(u)
type(rng_t), intent(inout) :: self
real :: u
integer :: imz
imz = self%state(1) - self%state(3)
if (imz < 0) imz = imz + 2147483579
self%state(1) = self%state(2)
self%state(2) = self%state(3)
self%state(3) = imz
self%state(4) = 69069 * self%state(4) + 1013904243
imz = imz + self%state(4)
u = 0.5d0 + 0.23283064d-9 * imz
end function rng_uniform
end module rng