From 040a78c3867e839001c95a56c2496403304640f1 Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Thu, 3 Aug 2023 19:38:19 -0700 Subject: [PATCH] fix: Remove usage of rand() and seed randomness for chatserver (#1152) * Remove usage of rand(), time(0) to time(NULL) Replace it with the random engine mt19937. convert time(0) to time(NULL) * update * revert a bunch of changes --- dChatServer/ChatServer.cpp | 3 +++ dScripts/BaseSurvivalServer.cpp | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index b9fb8556..185bccbf 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -33,6 +33,7 @@ namespace Game { dChatFilter* chatFilter = nullptr; AssetManager* assetManager = nullptr; bool shouldShutdown = false; + std::mt19937 randomEngine; } @@ -114,6 +115,8 @@ int main(int argc, char** argv) { Game::server = new dServer(Game::config->GetValue("external_ip"), ourPort, 0, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::Chat, Game::config, &Game::shouldShutdown); Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", bool(std::stoi(Game::config->GetValue("dont_generate_dcf")))); + + Game::randomEngine = std::mt19937(time(0)); //Run it until server gets a kill message from Master: auto t = std::chrono::high_resolution_clock::now(); diff --git a/dScripts/BaseSurvivalServer.cpp b/dScripts/BaseSurvivalServer.cpp index 0e8d0431..ffa86968 100644 --- a/dScripts/BaseSurvivalServer.cpp +++ b/dScripts/BaseSurvivalServer.cpp @@ -472,7 +472,7 @@ std::vector BaseSurvivalServer::GetRandomMobSet(SpawnerNetworkCollecti if (mobSets.sets.find(spawnerNetworkCollection.mobSetName) != mobSets.sets.end()) { auto mobSet = mobSets.sets.at(spawnerNetworkCollection.mobSetName); if (setNumber < mobSet.size()) { - return mobSet.at(setNumber).at(rand() % mobSet.at(setNumber).size()); + return mobSet.at(setNumber).at(GeneralUtils::GenerateRandomNumber(0, mobSet.at(setNumber).size() - 1)); } } @@ -487,7 +487,7 @@ SpawnerNetwork BaseSurvivalServer::GetRandomSpawner(SpawnerNetworkCollection& sp } if (!validSpawners.empty()) { - auto spawner = validSpawners.at(rand() % validSpawners.size()); + auto spawner = validSpawners.at(GeneralUtils::GenerateRandomNumber(0, validSpawners.size() - 1)); spawner.isActive = true; return spawner; }