    Program g05ndfe

!     G05NDF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05ndf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: genid, i, ifail, lstate, m, n, pm,   &
                                          subid
!     .. Local Arrays ..
      Integer, Allocatable             :: ipop(:), isampl(:), state(:)
      Integer                          :: seed(lseed)
!     .. Executable Statements ..
      Write (nout,*) 'G05NDF 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

      Allocate (ipop(n))

!     Display title
      Write (nout,99999) ' Samples from the first ', n, ' integers'
      Write (nout,*)
      Write (nout,*) ' Sample size     Values'

!     Initialize the population
      Do i = 1, n
        ipop(i) = i
      End Do

!     Dummy allocation
      Allocate (isampl(1))

!     Loop over different sample sizes
      pm = 0
d_lp: Do
        Read (nin,*,Iostat=ifail) m
        If (ifail/=0) Then
          Exit d_lp
        End If

!       Reallocate ISAMPL
        If (pm/=m) Then
          Deallocate (isampl)
          Allocate (isampl(m))
          pm = m
        End If

!       Generate sample
        ifail = 0
        Call g05ndf(ipop,n,isampl,m,state,ifail)

!       Display the results
        Write (nout,99998) m, isampl(1:m)
      End Do d_lp

99999 Format (1X,A,I0,A)
99998 Format (1X,I6,9X,8(1X,I3))
    End Program g05ndfe
