aboutsummaryrefslogtreecommitdiff
path: root/src/slalib/gresid.f_pcm
blob: b2c6f76d3dbfb1cc65670621aad721971f34617a (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
REAL FUNCTION sla_GRESID (S)
*+
*     - - - - - - -
*      G R E S I D
*     - - - - - - -
*
*  Generate pseudo-random normal deviate ( = 'Gaussian residual')
*  (single precision)
*
*  Given:
*     S      real     standard deviation
*
*  The results of many calls to this routine will be
*  normally distributed with mean zero and standard deviation S.
*
*  The Box-Muller algorithm is used.  This is described in
*  Numerical Recipes, section 7.2.
*
*  !!!  Microsoft Fortran dependent - calls the RAN routine   !!!
*  !!!  To seed the random-number generator, either call the  !!!
*  !!!  Microsoft SEED routine specifying some INTEGER*2      !!!
*  !!!  seed or call the function sla_RANDOM specifying some  !!!
*  !!!  REAL seed.                                            !!!
*
*  P.T.Wallace   Starlink   1 April 1993
*
*  Copyright (C) 1995 Rutherford Appleton Laboratory
*-

      IMPLICIT NONE

      REAL S

      REAL X,Y,RV,R,W,GNEXT,G
      LOGICAL FIRST

      SAVE GNEXT,RV,FIRST
      DATA FIRST / .TRUE. /



*  Second normal deviate of the pair available?
      IF (FIRST) THEN

*     No - generate two random numbers in range +/- 1
 1       CONTINUE
         CALL RANDOM(RV)               !!! PC
         X = 2.0*RV-1.0
         CALL RANDOM(RV)               !!! PC
         Y = 2.0*RV-1.0

*     Try again if not in unit circle
         R = X*X+Y*Y
         IF (R.GE.1.0) GO TO 1

*     Box-Muller transformation, generating two deviates
         W = SQRT(-2.0*LOG(R)/MAX(R,1E-20))
         GNEXT = X*W
         G = Y*W

*     Set flag to indicate availability of next deviate
         FIRST = .FALSE.
      ELSE

*     Return second deviate of the pair & reset flag
         G = GNEXT
         FIRST = .TRUE.
      END IF

*  Scale the deviate by the required standard deviation
      sla_GRESID = G*S

      END