NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program g05tgfe

!     G05TGF Example Program Text

!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05tgf, nag_wp, x04eaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, maxlr = 5000, nin = 5,    &
                                          nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: pmax
      Integer                          :: genid, ifail, k, ldx, lr, lstate, m, &
                                          mode, n, subid
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: p(:), r(:)
      Integer                          :: seed(lseed)
      Integer, Allocatable             :: state(:), x(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: int, maxval, real, sqrt
!     .. Executable Statements ..
      Write (nout,*) 'G05TGF Example Program Results'
      Write (nout,*)
      Flush (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 initializer 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

!     Read in the distribution parameters
      Read (nin,*) m, k

      ldx = n
      Allocate (x(ldx,k),p(k))

!     Read in probabilities
      Read (nin,*) p(1:k)

!     Use suggested value for LR
      pmax = maxval(p(1:k))
      lr = int(3.0E1_nag_wp+2.0E1_nag_wp*sqrt(real(m,                          &
        kind=nag_wp)*pmax*(1.0E0_nag_wp-pmax)))

!     If R is a reasonable size use MODE = 2
!     else do not reference R and use MODE = 3
      If (lr<maxlr) Then
        mode = 2
      Else
        mode = 3
        lr = 0
      End If

      Allocate (r(lr))

!     Generate the variates
      ifail = 0
      Call g05tgf(mode,n,m,k,p,r,lr,state,x,ldx,ifail)

!     Display the variates
      ifail = 0
      Call x04eaf('General',' ',n,k,x,ldx,' ',ifail)

    End Program g05tgfe