123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- # 0 "<stdin>"
- # 0 "<built-in>"
- # 0 "<command-line>"
- # 1 "/usr/include/stdc-predef.h" 1 3 4
- # 17 "/usr/include/stdc-predef.h" 3 4
- # 2 "<command-line>" 2
- # 1 "<stdin>"
- # 10 "<stdin>"
- 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
|