SUBROUTINE FRAN(N,NU1,NU2,ISTART,X)
C
C     PURPOSE--THIS SUBROUTINE GENERATES A RANDOM SAMPLE OF SIZE N
C              FROM THE F DISTRIBUTION
C              WITH INTEGER DEGREES OF FREEDOM
C              PARAMETERS = NU1 AND NU2.
C              THIS DISTRIBUTION IS DEFINED FOR ALL NON-NEGATIVE X.
C              THE PROBABILITY DENSITY FUNCTION IS GIVEN
C              IN THE REFERENCES BELOW. 
C     INPUT  ARGUMENTS--N      = THE DESIRED INTEGER NUMBER 
C                                OF RANDOM NUMBERS TO BE
C                                GENERATED.
C                     --NU1    = THE INTEGER DEGREES OF FREEDOM
C                                FOR THE NUMERATOR OF THE F RATIO.
C                     --NU2    = THE INTEGER DEGREES OF FREEDOM
C                                FOR THE DENOMINATOR OF THE F RATIO.
C                     --ISTART = AN INTEGER FLAG CODE WHICH 
C                                (IF SET TO 0) WILL START THE
C                                GENERATOR OVER AND HENCE
C                                PRODUCE THE SAME RANDOM SAMPLE
C                                OVER AND OVER AGAIN
C                                UPON SUCCESSIVE CALLS TO
C                                THIS SUBROUTINE WITHIN A RUN; OR
C                                (IF SET TO SOME INTEGER
C                                VALUE NOT EQUAL TO 0,
C                                LIKE, SAY, 1) WILL ALLOW
C                                THE GENERATOR TO CONTINUE
C                                FROM WHERE IT STOPPED
C                                AND HENCE PRODUCE DIFFERENT
C                                RANDOM SAMPLES UPON
C                                SUCCESSIVE CALLS TO
C                                THIS SUBROUTINE WITHIN A RUN.
C     OUTPUT ARGUMENTS--X      = A SINGLE PRECISION VECTOR
C                                (OF DIMENSION AT LEAST N)
C                                INTO WHICH THE GENERATED
C                                RANDOM SAMPLE WILL BE PLACED.
C     OUTPUT--A RANDOM SAMPLE OF SIZE N 
C             FROM THE F DISTRIBUTION
C             WITH DEGREES OF FREEDOM
C             PARAMETERS = NU1 AND NU2. 
C     PRINTING--NONE UNLESS AN INPUT ARGUMENT ERROR CONDITION EXISTS. 
C     RESTRICTIONS--THERE IS NO RESTRICTION ON THE MAXIMUM VALUE
C                   OF N FOR THIS SUBROUTINE.
C                 --NU1 SHOULD BE A POSITIVE INTEGER VARIABLE.
C                 --NU2 SHOULD BE A POSITIVE INTEGER VARIABLE.
C     OTHER DATAPAC   SUBROUTINES NEEDED--UNIRAN. 
C     FORTRAN LIBRARY SUBROUTINES NEEDED--ALOG, SQRT, SIN, COS.
C     MODE OF INTERNAL OPERATIONS--SINGLE PRECISION.
C     LANGUAGE--ANSI FORTRAN. 
C     REFERENCES--MOOD AND GRABLE, INTRODUCTION TO THE
C                 THEORY OF STATISTICS, 1963, PAGES 231-232.
C               --JOHNSON AND KOTZ, CONTINUOUS UNIVARIATE
C                 DISTRIBUTIONS--2, 1970, PAGES 75-93.
C               --HASTINGS AND PEACOCK, STATISTICAL
C                 DISTRIBUTIONS--A HANDBOOK FOR
C                 STUDENTS AND PRACTITIONERS, 1975,
C                 PAGE 64.
C     WRITTEN BY--JAMES J. FILLIBEN
C                 STATISTICAL ENGINEERING LABORATORY (205.03)
C                 NATIONAL BUREAU OF STANDARDS
C                 WASHINGTON, D. C. 20234
C                 PHONE:  301-921-2315
C     ORIGINAL VERSION--NOVEMBER  1975. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
      DIMENSION Y(2),Z(2)
      DATA PI/3.14159265358979/
C
      IPR=6
C
C     CHECK THE INPUT ARGUMENTS FOR ERRORS
C
      IF(N.LT.1)GOTO50
      IF(NU1.LE.0)GOTO60
      IF(NU2.LE.0)GOTO65
      GOTO90
   50 WRITE(IPR,5)
      WRITE(IPR,47)N
      RETURN
   60 WRITE(IPR,15) 
      WRITE(IPR,47)NU1
      RETURN
   65 WRITE(IPR,25) 
      WRITE(IPR,47)NU2
      RETURN
   90 CONTINUE
    5 FORMAT(1H , 91H***** FATAL ERROR--THE FIRST  INPUT ARGUMENT TO THE
     1 FRAN   SUBROUTINE IS NON-POSITIVE *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 FRAN   SUBROUTINE IS NON-POSITIVE *****)
   25 FORMAT(1H , 91H***** FATAL ERROR--THE THIRD  INPUT ARGUMENT TO THE
     1 FRAN   SUBROUTINE IS NON-POSITIVE *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      CALL UNIRAN(1,ISTART,Y) 
C
C     GENERATE N F RANDOM NUMBERS
C     USING THE DEFINITION THAT
C     A F VARIATE WITH NU1 AND NU2 DEGREES OF FREEDOM
C     EQUALS (CHS1/NU1)/(CHS2/NU2)
C     WHERE CHS1 IS A CHI-SQUARED VARIATE
C     WITH NU1 DEGREES OF FREEDOM,
C     AND   CHS2 IS A CHI-SQUARED VARIATE
C     WITH NU2 DEGREES OF FREEDOM.
C     FIRST GENERATE UNIFORM (0,1) RANDOM NUMBERS,
C     THEN GENERATE NORMAL RANDOM NUMBERS,
C     THEN CHI-SQUARED RANDOM NUMBERS WITH NU1 DEGREES
C     OF FREEDOM,
C     THEN CHI-SQUARED RANDOM NUMBERS WITH NU2 DEGREES
C     OF FREEDOM,
C     AND THEN FINALLY THE F RANDOM NUMBER.
C
      ANU1=NU1
      ANU2=NU2
      DO100I=1,N
C
      SUM=0.0
      DO200J=1,NU1,2
      CALL UNIRAN(2,1,Y)
      ARG1=-2.0*ALOG(Y(1))
      ARG2=2.0*PI*Y(2)
      Z(1)=(SQRT(ARG1))*(COS(ARG2))
      Z(2)=(SQRT(ARG1))*(SIN(ARG2))
      SUM=SUM+Z(1)*Z(1)
      IF(J.EQ.NU1)GOTO200
      SUM=SUM+Z(2)*Z(2)
  200 CONTINUE
      CHS1=SUM
C
      SUM=0.0
      DO300J=1,NU2,2
      CALL UNIRAN(2,1,Y)
      ARG1=-2.0*ALOG(Y(1))
      ARG2=2.0*PI*Y(2)
      Z(1)=(SQRT(ARG1))*(COS(ARG2))
      Z(2)=(SQRT(ARG1))*(SIN(ARG2))
      SUM=SUM+Z(1)*Z(1)
      IF(J.EQ.NU2)GOTO300
      SUM=SUM+Z(2)*Z(2)
  300 CONTINUE
      CHS2=SUM
C
      X(I)=(CHS1/ANU1)/(CHS2/ANU2)
C
  100 CONTINUE
C
      RETURN
      END