    PROGRAM nag_rand_discrete_ex01

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

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

      READ (nag_std_in,*)          ! skip heading in data file
      READ (nag_std_in,*) n        ! distribution size
      READ (nag_std_in,*) r        ! number of random numbers

      ALLOCATE (p(n),iv(r))        ! Allocate storage

      READ (nag_std_in,*) p        ! cdf

      ! Generating random integers in one step
      k = 0

      CALL nag_rand_seed_set(seed,k)

      val = 0
      pdf = .FALSE.

      CALL nag_rand_user_dist(p,val,pdf,iv=iv,seed=seed)

      WRITE (nag_std_out,fmt) 'User supplied CDF; one step approach', iv

      ! Generating random integers in two steps

      CALL nag_rand_user_dist(p,val,pdf,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) 'User supplied CDF; two steps approach', iv

      DEALLOCATE (iv,p)            ! Deallocate storage

      CALL nag_deallocate(ref)     ! Free structure allocated by NAG fl90

    END PROGRAM nag_rand_discrete_ex01
