      INTEGER I(1000),S(1000),R(1000),T,TT,A,K(1000)
      REAL U(1)
      DOUBLE PRECISION DSEED
      OPEN(2,FILE='CHNBIN.OUT')
      WRITE(*,*) 'ENTER 5-DIGIT SEED FOR RANDOM NUMBERS GENERATOR'
      READ(*,*) ISEED
      DSEED=ISEED
      WRITE(*,*) 'ENTER THE NUMBER OF SIMULATED EPIDEMICS'
      READ(*,*) MM
      WRITE(*,*) 'ENTER 1 FOR GREENWOOD MODEL, 2 FOR REED FROST MODEL'
      READ(*,*) MODEL
      IF(MODEL.EQ.1) WRITE(2,101)
  101 FORMAT(/' GREENWOOD MODEL')
      IF(MODEL.EQ.2) WRITE(2,102)
  102 FORMAT(/' REED-FROST MODEL')
      WRITE(*,*) 'ENTER PROBABILITY OF INFECTION'
      READ(*,*) P
      WRITE(2,103) P
  103 FORMAT(/' PROBABILITY OF INFECTION ='F6.3)
      Q=1.-P
      WRITE(*,*) 'ENTER GROUP SIZE'
      READ(*,*) N
      WRITE(*,*) 'ENTER NUMBER OF INITIAL INFECTIVES'
      READ(*,*) I(1)
      WRITE(2,104) I(1)
  104 FORMAT(/I5,' INITIAL INFECTIVES')
      S(1)=N-I(1)
      WRITE(2,105) S(1)
  105 FORMAT(/I5,' INITIAL SUSCEPTIBLES')
      RN=S(1)*P
      BRN=N*P
      WRITE(2,106) BRN,RN
  106 FORMAT(/' BASIC REPRODUCTION NUMBER =',F5.2//
     &        ' REPRODUCTION NUMBER =',F5.2)
      IF(MODEL.EQ.1) PNS=Q**S(1)
      IF(MODEL.EQ.2) PNS=Q**(I(1)*S(1))
      WRITE(2,107) PNS
  107 FORMAT(/' PROBABILITY OF NO SPREAD =',F6.3)
      M=0
      WRITE(2,111)
  111 FORMAT(/' EPIDEMIC   INFECTED         CHAIN'
     &       /' ---------------------------------------')
    1 M=M+1
      R(M)=I(1)
      DO 2 T=2,N
      I(T)=0
      IF(MODEL.EQ.1) QT=Q
      IF(MODEL.EQ.2) QT=Q**I(T-1)
      PT=1.-QT
      DO 3 J=1,S(T-1)
      CALL GGUBS(DSEED,1,U)
      IF(U(1).LE.PT) I(T)=I(T)+1
CCCCCC      WRITE(2,110) M,T,PT,J,U,I(T)
CCCCCC  110 FORMAT(2I5,F10.5,I5,F10.5,I5)
    3 CONTINUE
      S(T)=S(T-1)-I(T)
      R(M)=R(M)+I(T)
      IF(I(T)*S(T).EQ.0) GOTO 4
    2 CONTINUE
    4 TT=T
      WRITE(2,112) M,R(M),(I(T),T=1,TT)
  112 FORMAT(I6,I11,7X,20I3)
      IF(M.EQ.MM) GOTO 5
      GOTO 1
    5 WRITE(2,113)
  113 FORMAT(' --------------------------------------')
      DO 6 A=I(1),N
    6 K(A)=0
      RSUM=0
      DO 7 M=1,MM
      A=R(M)
      RSUM=RSUM+A
    7 K(A)=K(A)+1
      WRITE(2,114)
  114 FORMAT(//' DISTRIBUTION OF NO. INFECTED'
     &       //' INFECTED   FREQUENCY   PROPORTION'
     &        /' ---------------------------------')
      TM=MM
      DO 8 A=I(1),N
      PROP=K(A)/TM
      IF(K(A).GT.0) WRITE(2,115) A,K(A),PROP
    8 CONTINUE
  115 FORMAT(I6,I12,F13.3)
      WRITE(2,116)
  116 FORMAT(' ---------------------------------')
      WRITE(*,*)
      RMEAN=RSUM/TM
      WRITE(2,117) RMEAN
  117 FORMAT(/' MEAN NUMBER OF INFECTED ='F6.2)
      WRITE(*,*) 'OUTPUT ON FILE CHNBIN.OUT'
      STOP
      END

