      program rwalk02
c-----------------------------------------------------------------
c Random walk in 2 dimensions in a city with n*n blocks
c Goal: find average number of steps to get out the city
c       if the initial position is random in the city
c-----------------------------------------------------------------
c input from a keyboard
c    number of trials
c    city size N-blocks
c output screen
c    city size, number of trials, average way to get out (block)
c Attention!!!
c the code call subroutine 'gettim' to get current time
c that name is not unique, and may be different on different compilers
c-----------------------------------------------------------------
c written by: Alex Godunov
c last revision: October 2006
c-----------------------------------------------------------------
      implicit none
      integer*4 it, itests, in, ncity, init, x, y, nsteps, iway, out
      integer*4 stotal
      integer*4 ihour, imin, isec, imsec
      real*4 rand, asteps

      write (*,*) ' enter numbers of tests and city dimension'
      read  (*,*) itests, ncity

c initial seed number calculated from current time

      call gettim(ihour,imin,isec,imsec)
      init = imsec*isec*imin+ihour
      call srand(init)

      stotal = 0

c*** loop over trials

      do it=1,itests

c*** initial position in a n*n city
        x = (ncity+1)*rand()
        y = (ncity+1)*rand()

        nsteps = 0
        out = 0

        do while (out.eq.0)
            nsteps = nsteps+1
            iway = int(0.0+4.0*rand())
            if(iway.eq.0) x = x+1
            if(iway.eq.1) x = x-1
            if(iway.eq.2) y = y+1
            if(iway.eq.3) y = y-1
c check conditions to be out of n*n city
            if(x.lt.0.or.x.gt.ncity) out=1
            if(y.lt.0.or.y.gt.ncity) out=1
        end do
      stotal = stotal + nsteps
      end do
c*** average number of steps
      asteps = float(stotal)/float(itests)
      write (*,100) ncity, itests, asteps
100   format(' ncity =',i5,'  itests =',i7,' steps =',1pe12.3)
      stop
      end