      FUNCTION RANDOM()

C     This FORTRAN-77 function returns a REAL  pseudo-random  number  in
C     the range:  0.0 < RANDOM() < 1.0

C     The period of this function is  2,147,483,646  (2**31-2);  that is,
C     it will generate 2,147,483,636 pseudo-random numbers before repeat-
C     ing the series.

C     The seed for this generator can  be  set  by:   CALL SETRAN(SEED),
C     where SEED is an INTEGER in the range:   0 < SEED < 2,147,483,647;
C     the default starting seed is 1.

C     Examples:
C                   X = 2.0*RANDOM()-1.0
C                   IF (RANDOM().LT.0.5) CALL LOWER

C     In the first example,  X is assigned a random number in the  range
C     of:  -1.0 < X < +1.0.  In the second example,  subroutine LOWER is
C     called fifty percent of the time.  (i.e.  When the value  returned
C     by RANDOM() is less than one-half.)

C     Notes:

C             (1)  This function MUST be compiled and run on a  computer
C             which supports 32-bit, or larger, integers.

C             (2)  This function uses BLOCK DATA RANSEE  to  define  the
C             starting value for SEED.   If the function is not compiled
C             with BLOCK DATA RANSEE, the user MUST initialize SEED by a
C             call to SETRAN.

C     This function is based on a Pascal routine given in the  following
C     reference:

C     Park, Stephen K., &  Miller, Keith W.,  "Random Number Generators:
C     Good Ones are Hard to Find", in "Communications of the ACM",  Vol.
C     31, No. 10 (October 1988),  pp 1192 - 1201.  Abstract:  "Practical
C     and theoretical issues are presented concerning the design, imple-
C     mentation, and use of a good, minimal standard random number  gen-
C     erator that will port to virtually all systems."

C     Function coded by Harry M. Murphy on 31 October 1988.

      INTEGER A, HI, LO, M, Q, R, SEED, TEMP

      REAL RANDOM

      COMMON /RANCOM/ SEED

      SAVE /RANCOM/

C     A = the multiplier.  M = the modulus = 2**31-1.
C     Q = M div A.         R = M mod A.

      PARAMETER (A = 16807, M = 2147483647, Q = 127773, R = 2836)

C                   Execution of FUNCTION RANDOM() starts here.
      HI = SEED / Q
      LO = MOD(SEED,Q)
      TEMP = A*LO - R*HI
      IF (TEMP.LE.0)
     :  THEN
          SEED = TEMP + M
        ELSE
          SEED = TEMP
        END IF
      RANDOM = REAL(SEED) / M
      RETURN
      END
      SUBROUTINE SETRAN(SD)

C     Subroutine SETRAN initializes the random  number  seed,  SEED,  in
C     common block /RANCOM/, based on the INTEGER argument, SD, where SD
C     is in the range:  0 < SD < 2,147,483,647 .

C     SEED is used by the random number generator, FUNCTION RANDOM().

C     SEED is defined in BLOCK DATA RANSEE.

C     Subroutine coded by Harry M. Murphy on 31 October 1988.

      INTEGER MD, SD, SEED

      COMMON /RANCOM/ SEED

      SAVE /RANCOM/

      PARAMETER (MD = 2147483647)

C                   Execution of SETRAN starts here.
C                   If SD is in range, set SEED = SD,
C                   otherwise, set SEED = 1.
      IF ((SD.GT.0).AND.(SD.LT.MD))
     :  THEN
          SEED = SD
        ELSE
          SD = 1
        END IF
      RETURN
      END
      BLOCK DATA RANSEE

C     This block data routine defines the initial value of SEED, used in
C     the pseudo-random number generator, FUNCTION RANDOM().

C     Coded by Harry M. Murphy on 31 October 1988.

      INTEGER SEED

      COMMON /RANCOM/ SEED

      SAVE /RANDOM/

      DATA SEED/1/

      END
