    PROGRAM g05nbfe

!      G05NBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kbf, g05nbf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: n = 8, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, igen, k, m
!      .. Local Arrays ..
       INTEGER                         :: ipop(n), isampl(n), iseed(4)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05NBF Example Program Results'
       WRITE (nout,*)
!      Initialize the seed to a repeatable sequence
       iseed(1) = 1762543
       iseed(2) = 9324783
       iseed(3) = 1542344
       iseed(4) = 742355
!      IGEN identifies the stream.
       igen = 1
       CALL g05kbf(igen,iseed)
       WRITE (nout,99999) ' Samples from the first ', n, ' integers'
       WRITE (nout,*)
       WRITE (nout,*) ' Sample size     Values'
       DO i = 1, n
          ipop(i) = i
       END DO
       DO m = 1, n
          ifail = 1
          CALL g05nbf(ipop,n,isampl,m,igen,iseed,ifail)

          IF (ifail==0) THEN
             WRITE (nout,99998) m, (isampl(k),k=1,m)
          ELSE
             WRITE (nout,99997) ifail
             GO TO 20
          END IF
       END DO
20     CONTINUE

99999  FORMAT (1X,A,I1,A)
99998  FORMAT (1X,I6,10X,8I3)
99997  FORMAT (1X,' ** G05NBF returned with IFAIL = ',I5)
    END PROGRAM g05nbfe
