    PROGRAM nag_rand_discrete_ex04

      ! Example Program Text for nag_rand_discrete
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_out
      USE nag_rand_discrete, ONLY : nag_rand_seed_set, nag_rand_neg_binom, &
       nag_rand_ref_vec, nag_seed_wp => nag_seed_dp, &
       nag_ref_vec_wp => nag_ref_vec_dp, nag_deallocate
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: r = 5
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      CHARACTER (*), PARAMETER :: fmt = '(/1x,a/(1X, 8i5))'
      ! .. Local Scalars ..
      INTEGER :: k, n
      REAL (wp) :: p
      TYPE (nag_ref_vec_wp) :: ref
      TYPE (nag_seed_wp) :: seed
      ! .. Local Arrays ..
      INTEGER :: iv(r)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) &
       'Example Program Results for nag_rand_discrete_ex04'

      ! Generating random integers in one step
      k = 0

      CALL nag_rand_seed_set(seed,k)

      n = 50
      p = 0.5_wp

      CALL nag_rand_neg_binom(n,p,iv=iv,seed=seed)

      WRITE (nag_std_out,fmt) &
       'Negative binomial distribution: n=50, p=0.5, one step approach', iv

      ! Generating random integers in two steps

      CALL nag_rand_neg_binom(n,p,ref=ref) ! Create reference vector

      k = 0

      CALL nag_rand_seed_set(seed,k)

      iv = nag_rand_ref_vec(seed,ref,r)

      WRITE (nag_std_out,fmt) &
       'Negative binomial distribution: n=50, p=0.5, two steps approach', iv

      CALL nag_deallocate(ref)

    END PROGRAM nag_rand_discrete_ex04
