! This program needs the file  "ruti.f"  These are routines in Fortran 77
! To compile  f90 pro2.f90 ruti.f
!  If file links correctly
!  pro2.f90:
!  ruti.f:
!  Linking:
!  After running a.out generates samples of the M-H implementaion

      PROGRAM metro
       IMPLICIT NONE
       REAL :: gasdev, ran2,thnew, thold, delta, lratio, u 
       REAL :: m
       INTEGER ::  ix, i,N, accept
!
       open(1, file="samples.dat",status="unknown")
!     
       ix = 12345678
       PRINT *, " No. of simulations for M-H?"
       READ *, N
!
       PRINT *, "What is the initial value of the chain?"
       READ *, thold
!
       PRINT *, "What is the value of delta ?"
       READ *, delta

        accept =0
        m=0.0       
!
       do i=1,N
          PRINT *, i 
          thnew= thold + (2.0*delta*ran2(ix)-delta)
          lratio = (thold**2-thnew**2)/2.0
          u=ran2(ix)
          m= min(1.0,exp(lratio))
!
            if (u.le.m) then
              write(1,*) thnew
              accept = accept + 1
            else
             write(1,*) thold
            endif
       end do 
!
       close(1)
!
         PRINT *, "No of acceptances is"
         PRINT *, accept
!
       END PROGRAM metro

