module nbi_fbm_random
  use iso_c_binding, only: fp => c_double 
  !  module of utility variables used for generating random samples
  !  from distribution functions, in nubeam.
  use logmod
 
  implicit none
  private  :: fp
  integer, dimension(:), allocatable :: indxs,jb_a,jb_e,n_gc2
  real(fp), dimension(:), allocatable :: drndm,xz_id
  contains
 
    subroutine nbi_fbm_random_fetch(irsize,ierr)
!
!  allocate random sample arrays;
!  get sorted list of real*8 random numbers
!
      integer, intent(in) :: irsize  ! sample size
      integer, intent(out) :: ierr
!
      if(allocated(drndm)) deallocate(drndm,indxs,jb_a,jb_e,n_gc2,xz_id)
      allocate(indxs(irsize))
      allocate(drndm(irsize))
      allocate(jb_a(irsize),jb_e(irsize),n_gc2(irsize),xz_id(irsize))
      jb_a=0
      jb_e=0
      n_gc2=0
      xz_id=0.0_fp
      call nbi_dransort(drndm,indxs,irsize,ierr)
      if(ierr.ne.0) then
         call infolog( ' nbi_fbm_random_fetch: nbi_dransort error.')
      endif
 
    end subroutine nbi_fbm_random_fetch
 
  end module nbi_fbm_random
