!-----------------------------------------------------------------------
!  franay -- return an array of random no.s btw 0 and 1
!
subroutine franay(zran,iord,inum,iopt)
  !
  use nbi_com
  use nbi_random
  !
  !  input options
  !   iopt = 0 -- just return a sequence of random no.s; the iord array
  !               is not used
  !   iopt = 1 -- return sort order of random numbers in array iord
  !
  !  output
  !============
  implicit none
  integer :: iopt,inum,i,irn,isortd,iptr,nb_locord,imov
  !============
  real(fp) :: zran(inum)  ! array of inum random numbers
  integer :: iord(inum)  ! ordering of zran array (optional)
  !
  !-----------------------------------------
  !
  !  compute the rn's
  !
  do i=1,inum
     zran(i)=nbrandm(0)
  end do
  !
  if(iopt.eq.0) return
  !
  iord(1)=1
  if(inum.eq.1) return
  !
  !  compute the sorted order of rn's, store result in iord
  !
  do irn=2,inum
     isortd=irn-1
     iptr=nb_locord(zran,iord,isortd,zran(irn))
     if(iptr.le.isortd) then
        !  make room in order array
        do imov=isortd,iptr,-1
           iord(imov+1)=iord(imov)
        end do
     end if
  !  insert entry
     iord(iptr)=irn
  end do
  
  return
end subroutine franay
