From 6aa0e74d3e3b4fe6e9d7892caf55c2a987df82f3 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 11 May 2019 22:37:19 +0200 Subject: [PATCH 1/4] Fix util_rand only returning 15 bit values. --- src/openrct2/util/Util.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 0245e4158d..29d858ee3c 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -21,6 +21,9 @@ #include #include #include +#include + +static std::mt19937 _prng; int32_t squaredmetres_to_squaredfeet(int32_t squaredMetres) { @@ -528,13 +531,12 @@ bool str_is_null_or_empty(const char* str) void util_srand(int32_t source) { - srand(source); + _prng.seed(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(); + return _prng(); } #define CHUNK (128 * 1024) From 5198df5c25acfe2635d5d2999a7592ec48e11b0a Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 11 May 2019 22:38:57 +0200 Subject: [PATCH 2/4] Improve generation of random advertisment key --- src/openrct2/network/NetworkServerAdvertiser.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index 1b8623bfb4..cf90398753 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -27,6 +27,7 @@ # include # include +# include # include # ifndef DISABLE_HTTP @@ -241,10 +242,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, 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; From c6ea47c631b4f4e19099d3d41959fc6af312c5ef Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 12 May 2019 11:32:33 +0200 Subject: [PATCH 3/4] Make util_rand thread safe --- src/openrct2/Context.cpp | 1 - src/openrct2/util/Util.cpp | 8 +------- src/openrct2/util/Util.h | 1 - src/openrct2/world/MapGen.cpp | 2 -- 4 files changed, 1 insertion(+), 11 deletions(-) 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/util/Util.cpp b/src/openrct2/util/Util.cpp index 29d858ee3c..0cd47c5cfb 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -23,8 +23,6 @@ #include #include -static std::mt19937 _prng; - int32_t squaredmetres_to_squaredfeet(int32_t squaredMetres) { // 1 metre squared = 10.7639104 feet squared @@ -529,13 +527,9 @@ bool str_is_null_or_empty(const char* str) return str == nullptr || str[0] == 0; } -void util_srand(int32_t source) -{ - _prng.seed(source); -} - uint32_t util_rand() { + thread_local std::mt19937 _prng(std::random_device{}()); return _prng(); } 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; From eb3018b6a94b6207a4c91c84f82bb3e10035f36b Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 12 May 2019 11:32:41 +0200 Subject: [PATCH 4/4] Fix warnings --- src/openrct2/network/NetworkServerAdvertiser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/network/NetworkServerAdvertiser.cpp b/src/openrct2/network/NetworkServerAdvertiser.cpp index cf90398753..5658d25c31 100644 --- a/src/openrct2/network/NetworkServerAdvertiser.cpp +++ b/src/openrct2/network/NetworkServerAdvertiser.cpp @@ -244,7 +244,7 @@ private: }; std::random_device rd; - std::uniform_int_distribution<> dist(0, std::size(hexChars) - 1); + std::uniform_int_distribution dist(0, static_cast(std::size(hexChars) - 1)); char key[17]; for (int32_t i = 0; i < 16; i++)