    Program g05rgfe

!     G05RGF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05rgf, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: theta
      Integer                          :: genid, ifail, ldx, lstate, n, sdx,   &
                                          sorder, subid
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: x(:,:)
      Integer                          :: seed(lseed)
      Integer, Allocatable             :: state(:)
!     .. Executable Statements ..
      Write (nout,*) 'G05RGF 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 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 and order
      Read (nin,*) n, sorder

      If (sorder==1) Then
!       X(N,2)
        ldx = n
        sdx = 2
      Else
!       X(2,N)
        ldx = 2
        sdx = n
      End If
      Allocate (x(ldx,sdx))

!     Read in parameter
      Read (nin,*) theta

!     Generate variates
      ifail = 0
      Call g05rgf(n,theta,sorder,state,x,ldx,sdx,ifail)

!     Display the variates
      If (sorder==1) Then
!       X(N,2)
        ifail = 0
        Call x04caf('General',' ',n,2,x,ldx, &
          'Uniform variates with copula joint distribution',ifail)
      Else
!       X(2,N)
        ifail = 0
        Call x04caf('General',' ',2,n,x,ldx, &
          'Uniform variates with copula joint distribution',ifail)
      End If

    End Program g05rgfe
