c
      PROGRAM ran
      dimension num(32)

      write(*,*)'Enter Initial idum and N'
      read(*,*)idum,N

      sum=0.
      do i=1,N
        x=gasdev(idum)
c        write(*,*)i,idum,x
        write(1,*)i,idum,x
        x1=-4.
        do j=1,32
	  x2=x1+0.25
          if(x.gt.x1.and.x.le.x2)num(j)=num(j)+1
          x1=x2
        end do
        sum=sum+x
      end do
      write(*,*)'average=',sum/N

      x1=-4.
      do j=1,32
        x2=x1+0.25
        write(*,*)'x1,x2,x12,num',x1,x2,(x1+x2)/2.,num(j)
        write(2,*)x1,x2,(x1+x2)/2.,num(j)
        x1=x2
      end do

      stop
      end
c
c *** Randam Number with a normally distributed deviate with zero mean
c     and unit variance, using ran0 as the source of uniform deviates
c
      function gasdev(idum)
      save iset,gset
      data iset/0/

      if(iset.eq.0)then
   10   v1=2.*ran0(idum)-1.
        v2=2.*ran0(idum)-1.
        rsq=v1*v1+v2*v2
        if(rsq.ge.1..or.rsq.eq.0)goto 10
        fac=sqrt(-2.*log(rsq)/rsq)
        gset=v1*fac
        gasdev=v2*fac
        iset=1
      else
        gasdev=gset
        iset=0
      end if

      return
      end

c *** Randam Function **
c
      function ran0(idum)
      parameter(IA=16807,IM=2147483647,AM=1./IM,IQ=127773,IR=2836
     *         ,MASK=123459876)
c
      idum=ieor(idum,MASK)
      k=idum/IQ
      idum=IA*(idum-k*IQ)-IR*k
      if(idum.lt.0)idum=idum+IM
      ran0=AM*idum
      idum=ieor(idum,MASK)
c
      return
      end
