Uniform integer in [lo, hi] from a splitmix64_t generator.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(splitmix64_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 |
||
| type(error_t), | intent(inout), | optional | :: | err |
Set to |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=int64), | private | :: | span_minus_1 |
function splitmix64_next_range(gen, lo, hi, err) result(r) !! Uniform integer in `[lo, hi]` from a splitmix64_t generator. type(splitmix64_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), splitmix64_uniform_below(gen, span_minus_1)), default_int) end function splitmix64_next_range