Switch to a simpler & faster uniform real generation algorithm
This commit is contained in:
parent
754b279b1a
commit
be372ee007
1 changed files with 3 additions and 16 deletions
|
|
@ -52,25 +52,12 @@ namespace psemek::random
|
||||||
template <typename RNG>
|
template <typename RNG>
|
||||||
T uniform_real_distribution<T>::generate(RNG & rng)
|
T uniform_real_distribution<T>::generate(RNG & rng)
|
||||||
{
|
{
|
||||||
// see https://en.cppreference.com/w/cpp/numeric/random/generate_canonical
|
// 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...
|
||||||
static constexpr std::size_t digits = std::numeric_limits<T>::digits;
|
|
||||||
static T const limit = std::pow<T>(2, digits);
|
|
||||||
|
|
||||||
T const R = static_cast<T>(rng.max()) - static_cast<T>(rng.min()) + 1;
|
T const R = static_cast<T>(rng.max()) - static_cast<T>(rng.min()) + 1;
|
||||||
|
|
||||||
T mult = T(1);
|
return static_cast<T>(rng() - rng.min()) / R;
|
||||||
|
|
||||||
T sum = T(0);
|
|
||||||
|
|
||||||
while (mult < limit)
|
|
||||||
{
|
|
||||||
T inc = static_cast<T>(rng() - rng.min());
|
|
||||||
sum += inc * mult;
|
|
||||||
mult *= R;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum / mult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue