c     This is a multiplicative congruential random number generator
c     with modulus 2**31-1
c     Taken from the book "Monte Carlo: Concepts, Algorithms, and 
c     Applications" by George S. Fishman  P. 604

c     N = # of random numbers to be generated, <=NDIM
c     SEED = initial positive seed on entering, final seed on exit
c     DUNIF = vector of uniform (0,1) random numbers on exit
c     NDIM = dimension of DUNIF

c     SUBROUTINE RAND(N,SEED,DUNIF,NDIM)

      INTEGER N,NDIM
      REAL*8 SEED,DUNIF(1)
      REAL*8 DZ,DOVER,DZ1,DZ2,DOVER1,DOVER2
      REAL*8 DTWO31,DMDLS,DA1,DA2
      REAL*8 Q,T,XP,NUM,DEN

      OPEN (UNIT=20,FILE='OUT',STATUS='UNKNOWN')
      N = 1
      SEED = 98369
      NDIM = 1

      DATA DTWO31/2147483648D+0/,DMDLS/2147483647D+0/
      DATA DA1/41160.0D+0/,DA2/950665216.0D+0/

      DZ=SEED
      IF(N.GT.NDIM)N=NDIM
      DO 44 I=1,N
         DZ=IDINT(DZ)
         DZ1=DZ*DA1
         DZ2=DZ*DA2
         DOVER1=IDINT(DZ1/DTWO31)
         DOVER2=IDINT(DZ2/DTWO31)
         DZ1=DZ1-DOVER1*DTWO31
         DZ2=DZ2-DOVER2*DTWO31
         DZ=DZ1+DZ2+DOVER1+DOVER2
         DOVER=IDINT(DZ/DMDLS)
         DZ=DZ-DOVER*DMDLS
         DUNIF(I)=DZ/DMDLS
         SEED=DZ
 44   CONTINUE
      WRITE(20,*) DUNIF, SEED
      CLOSE(UNIT=20)
      RETURN
      END


