mtprng_rand_range Function

public function mtprng_rand_range(state, lo, hi) result(r)

NAME

mtprng_rand_range(3f) - [M_random:MERSENNE TWISTER] Obtain a pseudo-random integer in the range [lo,hi]
(LICENSE:CUSTOM OPEN)

SYNOPSIS

function mtprng_rand_range(state, lo, hi) result(r)
type(mtprng_state), intent(inout) :: state
integer, intent(in) :: lo
integer, intent(in) :: hi
integer(INT32) :: r

DESCRIPTION

Obtain a pseudo-random integer in the range [lo,hi]

OPTIONS

state  generator state initialized by mtprng_init(3f) or mtprng_init_array(3f)
lo     lowest value in desired range of values to return
hi     highest value in desired range of values to return

RETURNS

r      returned pseudo-random value in range from LO to HI

EXAMPLE

Sample program:

program demo_mtprng_rand_range
use M_random, only : mtprng_state, mtprng_init, mtprng_rand_range
use, intrinsic :: iso_fortran_env, only : int32
implicit none
integer(INT32) :: seed
type(mtprng_state) :: state
  GET_SEED: block
  integer :: count
     integer :: count_rate
     call system_clock(count, count_rate)
     seed = count
  endblock GET_SEED
  call mtprng_init(seed, state)
  write(*,*) mtprng_rand_range(state,20,30)
end program demo_mtprng_rand_range

Arguments

Type IntentOptional Attributes Name
type(mtprng_state), intent(inout) :: state
integer, intent(in) :: lo
integer, intent(in) :: hi

Return Value integer(kind=INT32)


Source Code

function mtprng_rand_range(state, lo, hi) result(r)

! ident_12="@(#) M_random mtprng_rand_range(3f) Obtain a pseudo-random integer in the range [lo hi]"

! arguments
type(mtprng_state), intent(inout) :: state
integer, intent(in) :: lo
integer, intent(in) :: hi
! return type
integer(INT32) :: r

   ! Use real value to calculate range
   r = lo + floor((hi - lo + 1.0_IEEE64) * mtprng_rand_real2(state))

end function mtprng_rand_range