    PROGRAM g05rafe

!      G05RAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g05kbf, g05raf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: ldc = 5, ldx = 100
       INTEGER, PARAMETER              :: lr = ldc*ldc + ldc + 1
       INTEGER, PARAMETER              :: nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, igen, j, m, mode, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: c(ldc,ldc), r(lr), x(ldx,ldc)
       INTEGER                         :: iseed(4)
!      .. Executable Statements ..
       WRITE (nout,*) 'G05RAF Example Program Results'
       WRITE (nout,*)

!      Initialize the seed to a repeatable sequence
       iseed(1) = 1762543
       iseed(2) = 9324783
       iseed(3) = 42344
       iseed(4) = 742355

!      Choose the random generator to use
       igen = 1

!      Initialize the random generator
       CALL g05kbf(igen,iseed)

!      Set the number of variables and variates
       m = 4
       n = 10

!      Input the upper triangle portion of the covariance matrix
       c(1,1) = 1.69E0_nag_wp
       c(1,2) = 0.39E0_nag_wp
       c(1,3) = -1.86E0_nag_wp
       c(1,4) = 0.07E0_nag_wp
       c(2,2) = 98.01E0_nag_wp
       c(2,3) = -7.07E0_nag_wp
       c(2,4) = -0.71E0_nag_wp
       c(3,3) = 11.56E0_nag_wp
       c(3,4) = 0.03E0_nag_wp
       c(4,4) = 0.01E0_nag_wp

       mode = 0
       ifail = 1

!      Set up reference vector and generate N numbers
       CALL g05raf(mode,m,c,ldc,n,x,ldx,igen,iseed,r,lr,ifail)

       IF (ifail==0) THEN
!         Display the results
          DO i = 1, n
             WRITE (nout,99999) (x(i,j),j=1,m)
          END DO
       ELSE
          WRITE (nout,99998) ifail
       END IF

99999  FORMAT (1X,10F10.4)
99998  FORMAT (1X,' ** G05RAF returned with IFAIL = ',I5)
    END PROGRAM g05rafe
