pcg32_next_range Function

private function pcg32_next_range(gen, lo, hi, err) result(r)

Uniform integer in [lo, hi] from a pcg32_t generator.

Arguments

Type IntentOptional Attributes Name
type(pcg32_t), intent(inout) :: gen

Generator, advanced by the draw.

integer(kind=default_int), intent(in) :: lo

Inclusive lower bound.

integer(kind=default_int), intent(in) :: hi

Inclusive upper bound; must not be below lo.

type(error_t), intent(inout), optional :: err

Set to ERROR_VALIDATION when hi < lo.

Return Value integer(kind=default_int)


Calls

proc~~pcg32_next_range~~CallsGraph proc~pcg32_next_range pcg32_next_range proc~error_set error_t%error_set proc~pcg32_next_range->proc~error_set proc~pcg32_uniform_below pcg32_uniform_below proc~pcg32_next_range->proc~pcg32_uniform_below proc~u64_add u64_add proc~pcg32_next_range->proc~u64_add proc~pcg32_next_u64 pcg32_t%pcg32_next_u64 proc~pcg32_uniform_below->proc~pcg32_next_u64 proc~rejection_mask~2 rejection_mask proc~pcg32_uniform_below->proc~rejection_mask~2 proc~u64_less u64_less proc~pcg32_uniform_below->proc~u64_less proc~pcg32_step pcg32_step proc~pcg32_next_u64->proc~pcg32_step proc~u64_shr u64_shr proc~rejection_mask~2->proc~u64_shr proc~pcg32_step->proc~u64_add proc~pcg32_step->proc~u64_shr proc~rotate_right32 rotate_right32 proc~pcg32_step->proc~rotate_right32 proc~u64_mul u64_mul proc~pcg32_step->proc~u64_mul

Called by

proc~~pcg32_next_range~~CalledByGraph proc~pcg32_next_range pcg32_next_range interface~next_range next_range interface~next_range->proc~pcg32_next_range

Variables

Type Visibility Attributes Name Initial
integer(kind=int64), private :: span_minus_1

Source Code

   function pcg32_next_range(gen, lo, hi, err) result(r)
      !! Uniform integer in `[lo, hi]` from a pcg32_t generator.
      type(pcg32_t), intent(inout) :: gen
         !! Generator, advanced by the draw.
      integer(default_int), intent(in) :: lo
         !! Inclusive lower bound.
      integer(default_int), intent(in) :: hi
         !! Inclusive upper bound; must not be below `lo`.
      type(error_t), intent(inout), optional :: err
         !! Set to `ERROR_VALIDATION` when `hi < lo`.
      integer(default_int) :: r

      integer(int64) :: span_minus_1

      r = lo
      if (hi < lo) then
         if (present(err)) then
            call err%set(ERROR_VALIDATION, "pic_random_dist: next_range requires hi >= lo")
         end if
         return
      end if
      if (hi == lo) return

      ! hi - lo in unsigned 64-bit, which is exact even when the span is wider
      ! than the signed range. Negation is spelled as `not(x) + 1` because
      ! negating the most negative value directly is signed overflow.
      span_minus_1 = u64_add(int(hi, int64), u64_add(not(int(lo, int64)), 1_int64))
      r = int(u64_add(int(lo, int64), pcg32_uniform_below(gen, span_minus_1)), default_int)
   end function pcg32_next_range