!====================================================================
program random_walks
 use, intrinsic        :: iso_fortran_env
 use                   :: drandom_mod
 implicit none 
 integer, parameter    :: dp    = real64
 integer               :: Nwalk,Nstep
 integer               :: ir
 integer(dp)           :: iwalk,istep,n
 integer(dp)           :: x,y
 real   (dp)           :: xr,yr
 integer               :: unit1,unit2
!--------------------------------------------------------------------
 call init
 do iwalk  = 1, Nwalk
  x = 0; y = 0; n = 0
  do istep = 1, Nstep
   ir  = INT(drandom()*4)
   select case(ir)
    case(0)
     x = x + 1
    case(1)
     x = x - 1
    case(2)
     y = y + 1
    case(3)
     y = y - 1
    end select
    if(iwalk <= 50) write(unit2,'(3I18)')iwalk,x,y
    if( x == 0 .and. y == 0) n = n + 1
  enddo !do istep=1,Nstep
  xr = DBLE(x); yr = DBLE(y)
  write(unit1,'(I12,G28.17,3I18)')iwalk,xr*xr+yr*yr,x,y,n
 enddo !do iwalk = 1, Nwalk
!--------------------------------------------------------------------
contains
!--------------------------------------------------------------------
 subroutine init
  character(20) :: arg
  integer       :: unit3
  !Read Nwalk and Nstep from command line arguments:
  if( iargc() .ne. 2)then
   print *,'Usage: rw  <Nwalk> <Nstep>'
   stop
  endif
  call getarg(1,arg); read(arg,*) Nwalk
  call getarg(2,arg); read(arg,*) Nstep
  !Seed drandom from /dev/urandom
  open(newunit=unit3, file='/dev/urandom', access='stream', & 
       form='unformatted')
  read (unit3) seed
  close(unit3)

  open (newunit=unit1,file='dataR')
  open (newunit=unit2,file='data' )

 end subroutine init
!--------------------------------------------------------------------
end program random_walks
!====================================================================

!  ---------------------------------------------------------------------
!  Copyright by Konstantinos N. Anagnostopoulos (2004-2022)
!  Physics Dept., National Technical University,
!  konstant@mail.ntua.gr, www.physics.ntua.gr/~konstant
!  
!  This program is free software: you can redistribute it and/or modify
!  it under the terms of the GNU General Public License as published by
!  the Free Software Foundation, version 3 of the License.
!  
!  This program is distributed in the hope that it will be useful, but
!  WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
!  General Public License for more details.
!  
!  You should have received a copy of the GNU General Public Liense along
!  with this program.  If not, see <http://www.gnu.org/licenses/>.
!  -----------------------------------------------------------------------
