ccccccccccccccccccccccccc     Program 9.1     cccccccccccccccccccccccccc
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c                                                                      c
c Please Note:                                                         c
c                                                                      c
c (1) This computer program is part of the book, "An Introduction to   c
c     Computational Physics," written by Tao Pang and published and    c
c     copyrighted by Cambridge University Press in 1997.               c
c                                                                      c
c (2) No warranties, express or implied, are made for this program.    c
c                                                                      c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
      PROGRAM MCDS
C
C Integration with the direct sampling Monte Carlo scheme.
C The integrand is f(x) = x*x.
C
      PARAMETER (M=1000000)
      COMMON /CSEED/ ISEED
      INTEGER*4 time,STIME,T(9)
C
C Initial seed from the system time and forced to be odd
C
      STIME = time(%REF(0))
      CALL gmtime(STIME,T)
      ISEED = T(6)+70*(T(5)+12*(T(4)
     *            +31*(T(3)+23*(T(2)+59*T(1)))))
      IF (MOD(ISEED,2).EQ.0) ISEED = ISEED-1
C
      SUM1 = 0.0
      SUM2 = 0.0
      DO 100 J = 1, M
        X = RANF()
        SUM1 = SUM1+F(X)
        SUM2 = SUM2+F(X)**2
  100 CONTINUE
      S  = SUM1/M
      DS = SQRT(ABS(SUM2/M-(SUM1/M)**2)/M)
      WRITE(6,999) S,DS
      STOP
  999 FORMAT (2F14.8)
      END
C
      FUNCTION F(X)
        F = X*X
      RETURN
      END
C
      FUNCTION RANF()
      DATA IA/16807/,IC/2147483647/,IQ/127773/,IR/2836/
      COMMON /CSEED/ ISEED
        IH = ISEED/IQ
        IL = MOD(ISEED,IQ)
        IT = IA*IL-IR*IH
        IF(IT.GT.0) THEN
          ISEED = IT
        ELSE
          ISEED = IC+IT
        END IF
        RANF = ISEED/FLOAT(IC)
      RETURN
      END
