! This is a nag library test program.
! scp uniformdiscrete.f90 yourcsname@rock:.
! ssh yourcsname@rock
! and run:
! f95 -lnag -o uniformdiscrete uniformdiscrete.f90
! ./uniformdiscrete
!
! to see 30 random integers from -1 to 1 
! generated for You by a NAG library routine.

PROGRAM main
  
  IMPLICIT NONE
  
  INTEGER G05DYF
  EXTERNAL G05DYF		
  
  INTEGER, DIMENSION(30) :: taulukko
  INTEGER :: i
  
  ! initializing the random generator, so that some randomity can occur...
  ! initialize it seeded from the system clock reading.
  CALL random_init()

  DO i=1, 30
     ! generate a discrete random number from -1 to 1
     taulukko(i) =G05DYF(-1,1)
  END DO

  WRITE(*,*) taulukko
  
CONTAINS
  SUBROUTINE random_init()
    ! This subroutine initializes the NAG random number generator
    ! from the current system clock reading.
    IMPLICIT NONE

    EXTERNAL G05CBF, G05ZAF
    ! Used for NAG random generation method choice (G05ZAF)
    ! and random generator initialization (G05CBF)

    INTEGER :: size, status
    INTEGER, DIMENSION(8) :: time
    INTEGER, DIMENSION(:), ALLOCATABLE :: seed
    
    CALL RANDOM_SEED(size=size)
    ALLOCATE(seed(size), stat=status)
    IF (status > 0) STOP 'Failed to allocate!'
    CALL DATE_AND_TIME(values = time)
    seed = 100*time(7) + time(8)/10
    CALL G05ZAF('O')
    CALL G05CBF(seed)
    
  END SUBROUTINE random_init
END PROGRAM main

