C----ROB SALGADO (cps615g)
C    1a.f  09/27/92 (09/28/92 PROBLEM 1.1a)
C
C    USE MONTE-CARLO TO CALCULATE THE VOLUME OF A CIRCLE
C

      program pimonte
      integer iseed,n,r,hit
      real x,y,montepi(1000),sum,mean,sumdevsq,truepi

      open (unit=3,file='pi')
      write(3,*) 'Pi Monte-Carlo [ROB SALGADO]'
      print*, 'Pi Monte-Carlo [ROB SALGADO]'

      iseed=0
      
      print*, 'Number of samples per run?'
      read*, n
 10   print*, 'Number of runs (up to 1000)?'
      read*, r
      if (r.gt.1000) goto 10
      
C----------------------------------------------- r RUNS
      sum=0.0
      write(3,*) 'Trial    Pi'
      do 200 j=1,r,1
         hit=0
C------------------------------------ n SAMPLES
      do 100 i=1,n,1
         x=rand(iseed)
         y=rand(iseed)
         if (x*x+y*y.gt.1.0) goto 100
         hit=hit+1
 100  continue
C----------------------------------------------|
      montepi(j)=4.0*hit/n
      write(3,*) j, montepi(j)
      sum=sum+montepi(j)
 200  continue
C------------------------------------------------------|

C----------------------------STATISTICS      
      mean=sum/r
      write(3,*) '----'
      write(3,*) r,' Runs, each with',n,' samples'
      sumdevsq=0.0
      do 300 j=1,r,1
         sumdevsq=sumdevsq+(mean-montepi(j))*(mean-montepi(j))
 300  continue
      write(3,*) 'Mean      =', mean, '  Std Dev=', sqrt(sumdevsq/n)
      truepi=3.1415926535
      write(3,*) 'True value=', truepi
      write(3,*) '% Error   =', 100.0*(mean-truepi)/truepi
      end

