    PROGRAM g05ncfe

!      G05NCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kff, g05ncf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: lseed = 1, nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: genid, i, ifail, j, lstate, n,       &
                                          nperm, subid
!      .. Local Arrays ..
       INTEGER, ALLOCATABLE            :: indx(:), state(:)
       INTEGER                         :: seed(lseed)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05NCF 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 and number of permutations
       READ (nin,*) n, nperm

       ALLOCATE (indx(n))

       WRITE (nout,99998) nperm, ' Permutations of first ', n, ' integers'
       WRITE (nout,*)

!      Permutate NPERM times
       DO j = 1, nperm
!         Set up the index vector
          DO i = 1, n
             indx(i) = i
          END DO

!         Call the permutation routine
          ifail = 0
          CALL g05ncf(indx,n,state,ifail)

!         Display the results
          WRITE (nout,99999) indx(1:n)
       END DO

99999  FORMAT (1X,8I3)
99998  FORMAT (1X,I0,A,I0,A)
    END PROGRAM g05ncfe
