forked from nansencenter/enkf-topaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_random.F90
51 lines (41 loc) · 1.05 KB
/
m_random.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
50
51
module m_random
contains
subroutine random(work1,n)
! Returns a vector of random values N(variance=1,mean=0)
implicit none
integer, intent(in) :: n
real, intent(out) :: work1(n)
real, allocatable :: work2(:)
real, parameter :: pi=3.141592653589
allocate (work2(n))
call random_number(work1)
call random_number(work2)
work1= sqrt(-2.0*log(work1))*cos(2.0*pi*work2)
deallocate(work2)
end subroutine random
subroutine randn(n, vect)
implicit none
integer, intent(in) :: n
real, intent(out) :: vect(n)
integer :: i
real :: a(2), r
i = 0
do while (i < n)
call random_number(a)
a = 2.0 * a - 1.0
r = a(1) * a(1) + a(2) * a(2)
if (r > 1.0) then
cycle
end if
i = i + 1
! assume that r is never equal to 0 - PS
r = sqrt(-2.0 * log(r) / r);
vect(i) = r * a(1);
if (i == n) then
exit
end if
i = i + 1
vect(i) = r * a(2);
end do
end subroutine randn
end module m_random