-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.f90
33 lines (26 loc) · 836 Bytes
/
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
! function to generate random gaussian distribution
real*8 FUNCTION random_normal()
! Local variables
REAL*8 :: s = 0.449871, t = -0.386595, a = 0.19600, b = 0.25472, half=0.5, &
r1 = 0.27597, r2 = 0.27846, u, v, x, y, q
integer :: temp,i
! Generate P = (u,v) uniform in rectangle enclosing acceptance region
DO
CALL RANDOM_NUMBER(u)
CALL RANDOM_NUMBER(v)
v = 1.7156 * (v - half)
! Evaluate the quadratic form
x = u - s
y = ABS(v) - t
q = x**2 + y*(a*y - b*x)
! Accept P if inside inner ellipse
IF (q < r1) EXIT
! Reject P if outside outer ellipse
IF (q > r2) CYCLE
! Reject P if outside acceptance region
IF (v**2 < -4.0*LOG(u)*u**2) EXIT
END DO
! Return ratio of P's coordinates as the normal deviate
random_normal = v/u
RETURN
END FUNCTION random_normal