3#ifndef TETL_RANDOM_GENERATE_CANONICAL_HPP
4#define TETL_RANDOM_GENERATE_CANONICAL_HPP
16[[nodiscard]]
constexpr auto generate_canonical_iterations(
int bits,
uint64_t gMin,
uint64_t gMax) ->
int
22 auto const range = (gMax - gMin) + 1;
23 auto const target = ~uint64_t{0} >> (64 - bits);
26 auto ceiling =
int{0};
28 while (product <= target) {
43template <
typename Real,
size_t Bits,
typename RNG>
47 constexpr auto minBits =
static_cast<int>(digits < Bits ? digits : Bits);
49 auto const r = (
static_cast<Real
>(RNG::max()) -
static_cast<Real
>(RNG::min())) + Real{1};
50 auto const k = detail::generate_canonical_iterations(minBits, RNG::min(), RNG::max());
52 auto result = Real{0};
53 auto factor = Real{1};
55 for (
int i = 0; i < k; ++i) {
56 result += (
static_cast<Real
>(g()) - RNG::min()) * factor;
60 return result / factor;
constexpr auto generate_canonical(RNG &g) noexcept(noexcept(g())) -> Real
Generates a random floating point number in range [0,1).
Definition generate_canonical.hpp:44
Definition adjacent_find.hpp:8
TETL_BUILTIN_UINT64 uint64_t
Unsigned integer type with width of exactly 64 bits.
Definition uint_t.hpp:20
static constexpr int digits
Definition numeric_limits.hpp:24
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21