From 5583573beb6fd529d31884d2cb7e9465bea151f6 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 29 Mar 2026 21:01:51 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20eliminate=20last=20std::rand()=20calls?= =?UTF-8?q?=20=E2=80=94=20music=20shuffle=20and=20UI=20weather?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit zone_manager.cpp used std::rand() for music track selection with modulo bias and global state. game_screen.cpp used std::rand() for rain/snow particle positions. Both now use local std::mt19937 seeded from random_device. Also removes the global srand(time(nullptr)) call since no code depends on the C rand() seed anymore. No std::rand() or srand() calls remain in the codebase. --- src/game/zone_manager.cpp | 6 ++++-- src/ui/game_screen.cpp | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/game/zone_manager.cpp b/src/game/zone_manager.cpp index 10921baf..6ace1a60 100644 --- a/src/game/zone_manager.cpp +++ b/src/game/zone_manager.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace wowee { @@ -409,7 +410,7 @@ void ZoneManager::initialize() { tileToZone[14 * 100 + 16] = 1657; tileToZone[14 * 100 + 17] = 1657; - std::srand(static_cast(std::time(nullptr))); + // Seed removed — music shuffle now uses a local mt19937 (see pickMusicTrack). LOG_INFO("Zone manager initialized: ", zones.size(), " zones, ", tileToZone.size(), " tile mappings"); } @@ -458,7 +459,8 @@ std::string ZoneManager::getRandomMusic(uint32_t zoneId) { // Avoid playing the same track back-to-back const std::string* pick = pool[0]; for (int attempts = 0; attempts < 5; ++attempts) { - pick = pool[std::rand() % pool.size()]; + static std::mt19937 musicRng(std::random_device{}()); + pick = pool[std::uniform_int_distribution(0, pool.size() - 1)(musicRng)]; if (*pick != lastPlayedMusic_) break; } lastPlayedMusic_ = *pick; diff --git a/src/ui/game_screen.cpp b/src/ui/game_screen.cpp index cd13e332..e89aab46 100644 --- a/src/ui/game_screen.cpp +++ b/src/ui/game_screen.cpp @@ -23683,6 +23683,13 @@ void GameScreen::renderWeatherOverlay(game::GameHandler& gameHandler) { float sh = io.DisplaySize.y; if (sw <= 0.0f || sh <= 0.0f) return; + // Seeded RNG for weather particle positions — replaces std::rand() which + // shares global state and has modulo bias. + static std::mt19937 wxRng(std::random_device{}()); + auto wxRandInt = [](int maxExcl) { + return std::uniform_int_distribution(0, std::max(0, maxExcl - 1))(wxRng); + }; + ImDrawList* dl = ImGui::GetForegroundDrawList(); const float dt = std::min(io.DeltaTime, 0.05f); // cap delta at 50ms to avoid teleporting particles @@ -23701,8 +23708,8 @@ void GameScreen::renderWeatherOverlay(game::GameHandler& gameHandler) { if (!rs.initialized || rs.lastType != wType || rs.lastW != sw || rs.lastH != sh) { for (int i = 0; i < MAX_DROPS; ++i) { - rs.x[i] = static_cast(std::rand() % (static_cast(sw) + 200)) - 100.0f; - rs.y[i] = static_cast(std::rand() % static_cast(sh)); + rs.x[i] = static_cast(wxRandInt(static_cast(sw) + 200)) - 100.0f; + rs.y[i] = static_cast(wxRandInt(static_cast(sh))); } rs.initialized = true; rs.lastType = wType; @@ -23727,7 +23734,7 @@ void GameScreen::renderWeatherOverlay(game::GameHandler& gameHandler) { rs.y[i] += fallSpeed * dt; if (rs.y[i] > sh + 10.0f) { rs.y[i] = -10.0f; - rs.x[i] = static_cast(std::rand() % (static_cast(sw) + 200)) - 100.0f; + rs.x[i] = static_cast(wxRandInt(static_cast(sw) + 200)) - 100.0f; } if (rs.x[i] > sw + 100.0f) rs.x[i] -= sw + 200.0f; dl->AddLine(ImVec2(rs.x[i], rs.y[i]), @@ -23759,9 +23766,9 @@ void GameScreen::renderWeatherOverlay(game::GameHandler& gameHandler) { if (!ss.initialized || ss.lastW != sw || ss.lastH != sh) { for (int i = 0; i < MAX_FLAKES; ++i) { - ss.x[i] = static_cast(std::rand() % static_cast(sw)); - ss.y[i] = static_cast(std::rand() % static_cast(sh)); - ss.phase[i] = static_cast(std::rand() % 628) * 0.01f; + ss.x[i] = static_cast(wxRandInt(static_cast(sw))); + ss.y[i] = static_cast(wxRandInt(static_cast(sh))); + ss.phase[i] = static_cast(wxRandInt(628)) * 0.01f; } ss.initialized = true; ss.lastW = sw; @@ -23782,7 +23789,7 @@ void GameScreen::renderWeatherOverlay(game::GameHandler& gameHandler) { ss.phase[i] += dt * 0.25f; if (ss.y[i] > sh + 5.0f) { ss.y[i] = -5.0f; - ss.x[i] = static_cast(std::rand() % static_cast(sw)); + ss.x[i] = static_cast(wxRandInt(static_cast(sw))); } if (ss.x[i] < -5.0f) ss.x[i] += sw + 10.0f; if (ss.x[i] > sw + 5.0f) ss.x[i] -= sw + 10.0f;