    PROGRAM g05tkfe

!      G05TKF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kff, g05tkf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: lseed = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: genid, i, ifail, lstate, m, n, subid
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: lambda(:)
       INTEGER                         :: seed(lseed)
       INTEGER, ALLOCATABLE            :: state(:), x(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05TKF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

!      Read in the base generator information and seed
       READ (nin,*) genid, subid, seed(1)

!      Initial call to initialiser to get size of STATE array
       lstate = 0
       ALLOCATE (state(lstate))
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Reallocate STATE
       DEALLOCATE (state)
       ALLOCATE (state(lstate))

!      Initialize the generator to a repeatable sequence
       ifail = 0
       CALL g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!      Read in sample size
       READ (nin,*) n

!      Read in the distribution parameters
       READ (nin,*) m

       ALLOCATE (x(n),lambda(m))

!      Read in rest of distribution parameters
       READ (nin,*) lambda(1:m)

!      Generate N sets of the M variates
       DO i = 1, n
          ifail = 0
          CALL g05tkf(m,lambda,state,x,ifail)

!         Display the variates
          WRITE (nout,99999) i, x(1:m)
       END DO

99999  FORMAT (1X,6(1X,I12))
    END PROGRAM g05tkfe
