diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 11674063fe..59382c20d9 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -426,7 +426,6 @@ namespace OpenRCT2 } gScenarioTicks = 0; - util_srand((uint32_t)time(nullptr)); input_reset_place_obj_modifier(); viewport_init_all(); diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index bf9c921c51..9b857ce525 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -30,6 +30,7 @@ # include # include # include +# include # include enum MASTER_SERVER_STATUS @@ -307,10 +308,14 @@ private: static constexpr char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; + + std::random_device rd; + std::uniform_int_distribution dist(0, static_cast(std::size(hexChars) - 1)); + char key[17]; for (int32_t i = 0; i < 16; i++) { - int32_t hexCharIndex = util_rand() % std::size(hexChars); + int32_t hexCharIndex = dist(rd); key[i] = hexChars[hexCharIndex]; } key[std::size(key) - 1] = 0; diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 0245e4158d..0cd47c5cfb 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -21,6 +21,7 @@ #include #include #include +#include int32_t squaredmetres_to_squaredfeet(int32_t squaredMetres) { @@ -526,15 +527,10 @@ bool str_is_null_or_empty(const char* str) return str == nullptr || str[0] == 0; } -void util_srand(int32_t source) -{ - srand(source); -} - -// Caveat: rand() might only return values up to 0x7FFF, which is the minimum specified in the C standard. uint32_t util_rand() { - return rand(); + thread_local std::mt19937 _prng(std::random_device{}()); + return _prng(); } #define CHUNK (128 * 1024) diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 3fc0d2f1f0..ec046e8036 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -50,7 +50,6 @@ char* strcasestr(const char* haystack, const char* needle); bool utf8_is_bom(const char* str); bool str_is_null_or_empty(const char* str); -void util_srand(int32_t source); uint32_t util_rand(); uint8_t* util_zlib_deflate(const uint8_t* data, size_t data_in_size, size_t* data_out_size); diff --git a/src/openrct2/world/MapGen.cpp b/src/openrct2/world/MapGen.cpp index 33dbba1254..b624879531 100644 --- a/src/openrct2/world/MapGen.cpp +++ b/src/openrct2/world/MapGen.cpp @@ -136,8 +136,6 @@ void mapgen_generate(mapgen_settings* settings) int32_t x, y, mapSize, floorTexture, wallTexture, waterLevel; TileElement* tileElement; - util_srand((int32_t)platform_get_ticks()); - mapSize = settings->mapSize; floorTexture = settings->floor; wallTexture = settings->wall;