From 497c6efc5555f3c45b7e092b461f39a3d89de865 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Fri, 2 Aug 2024 11:53:59 +1000 Subject: [PATCH] Fix RNG bugs from upgrade to C++17 staticRandomShuffle now uses its own tiny impl of the deprecated std::random_shuffle, producing identical results in testing --- source/core/StarRandom.hpp | 4 ++-- source/core/StarStaticRandom.hpp | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/source/core/StarRandom.hpp b/source/core/StarRandom.hpp index a67a2c5..6afb2ad 100644 --- a/source/core/StarRandom.hpp +++ b/source/core/StarRandom.hpp @@ -208,8 +208,8 @@ typename Container::value_type Random::randValueFrom( template void Random::shuffle(Container& container) { - size_t max = container.size(); - std::shuffle(container.begin(), container.end(), URBG([max]() { return Random::randUInt(max - 1); })); + RandomSource random; + std::shuffle(container.begin(), container.end(), URBG([&]() { return static_cast(random.randu64()); })); } } diff --git a/source/core/StarStaticRandom.hpp b/source/core/StarStaticRandom.hpp index 1b355c4..d95463e 100644 --- a/source/core/StarStaticRandom.hpp +++ b/source/core/StarStaticRandom.hpp @@ -134,11 +134,14 @@ private: template void staticRandomShuffle(Container& container, T const& d, TL const&... rest) { - int mix = 0; - size_t max = container.size(); - std::shuffle(container.begin(), container.end(), URBG([&]() { - return staticRandomU32Range(0, max - 1, ++mix, d, rest...); - })); + auto begin = container.begin(); + auto end = container.end(); + auto it = begin; + for (int i = 1, mix = 0; ++it != end; ++i) { + int off = staticRandomU32Range(0, i, ++mix, d, rest...); + if (off != i) + std::swap(*it, *(begin + off)); + } } }