diff --git a/libs/random/include/psemek/random/uniform_real.hpp b/libs/random/include/psemek/random/uniform_real.hpp index 377197dc..531d7524 100644 --- a/libs/random/include/psemek/random/uniform_real.hpp +++ b/libs/random/include/psemek/random/uniform_real.hpp @@ -52,25 +52,12 @@ namespace psemek::random template T uniform_real_distribution::generate(RNG & rng) { - // see https://en.cppreference.com/w/cpp/numeric/random/generate_canonical - - static constexpr std::size_t digits = std::numeric_limits::digits; - static T const limit = std::pow(2, digits); + // Previously used an algorithm like in https://en.cppreference.com/w/cpp/numeric/random/generate_canonical + // But the simple algorithm here is way faster, so... T const R = static_cast(rng.max()) - static_cast(rng.min()) + 1; - T mult = T(1); - - T sum = T(0); - - while (mult < limit) - { - T inc = static_cast(rng() - rng.min()); - sum += inc * mult; - mult *= R; - } - - return sum / mult; + return static_cast(rng() - rng.min()) / R; } }