-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.f90
49 lines (39 loc) · 1.34 KB
/
utils.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
48
49
!> @author
!> Sam Hatfield, AOPP, University of Oxford
!> @brief
!> Contains utility functions.
module utils
use params
implicit none
private
public time_seed, randn
contains
!> @brief
!> Seeds random number generator based on the time.
subroutine time_seed()
integer :: i, n, clock
integer, allocatable :: seed(:)
call random_seed(size = n)
allocate(seed(n))
call system_clock(count=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
call random_seed(put = seed)
deallocate(seed)
end subroutine
!> @brief
!> Generates a random number drawn for the specified normal
!> distribution.
!> @param mean the mean of the distribution to draw from
!> @param stdev the standard deviation of the distribution to draw from
!> @return randn the generated random number
function randn(mean, stdev)
real(dp), intent(in) :: mean, stdev
real(dp) :: u, v, randn
real(dp) :: rand(2)
call random_number(rand)
! Box-Muller method
u = (-2.0_dp * log(rand(1))) ** 0.5_dp
v = 2.0_dp * 6.28318530718_dp * rand(2)
randn = mean + stdev * u * sin(v)
end function randn
end module