fix: eliminate last std::rand() calls — music shuffle and UI weather

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.
This commit is contained in:
Kelsi 2026-03-29 21:01:51 -07:00
parent a55eacfe70
commit 5583573beb
2 changed files with 18 additions and 9 deletions

View file

@ -4,6 +4,7 @@
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <random>
#include <unordered_set>
namespace wowee {
@ -409,7 +410,7 @@ void ZoneManager::initialize() {
tileToZone[14 * 100 + 16] = 1657;
tileToZone[14 * 100 + 17] = 1657;
std::srand(static_cast<unsigned>(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<size_t>(0, pool.size() - 1)(musicRng)];
if (*pick != lastPlayedMusic_) break;
}
lastPlayedMusic_ = *pick;

View file

@ -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<int>(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<float>(std::rand() % (static_cast<int>(sw) + 200)) - 100.0f;
rs.y[i] = static_cast<float>(std::rand() % static_cast<int>(sh));
rs.x[i] = static_cast<float>(wxRandInt(static_cast<int>(sw) + 200)) - 100.0f;
rs.y[i] = static_cast<float>(wxRandInt(static_cast<int>(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<float>(std::rand() % (static_cast<int>(sw) + 200)) - 100.0f;
rs.x[i] = static_cast<float>(wxRandInt(static_cast<int>(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<float>(std::rand() % static_cast<int>(sw));
ss.y[i] = static_cast<float>(std::rand() % static_cast<int>(sh));
ss.phase[i] = static_cast<float>(std::rand() % 628) * 0.01f;
ss.x[i] = static_cast<float>(wxRandInt(static_cast<int>(sw)));
ss.y[i] = static_cast<float>(wxRandInt(static_cast<int>(sh)));
ss.phase[i] = static_cast<float>(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<float>(std::rand() % static_cast<int>(sw));
ss.x[i] = static_cast<float>(wxRandInt(static_cast<int>(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;