mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 21:24:58 +01:00
fix: whole-number bounds of Random() give a real number again (#416)
* fix: whole-number bounds of Random() give a real number againee3af0bf(#99, in 26.05.2) renamed RandomInt to an overload, Random(int, int). The calls with whole-number bounds that expect a real number in between took the overload and draw whole numbers since: - wheel flats given with P0 happen to 1 vehicle in 101 (before: none), the random part of a flat is a whole number of millimetres; - the chance of truck hunting is h/101 instead of h/100; - the wind starts with a whole azimuth, speed and rates of change, and its direction changes in whole degrees per second; - the tachometer below 5 km/h jumps to whole numbers. The whole-number draw is RandomInt again, keeping the shared engine and the swap of inverted bounds; the loading screen's trivia use it. Those calls take the real Random() again, as up to 26.05.1. * RandomInt: the comment says what it draws and why it has a name of its own The comment put the bug on the overloading. It was in the calls that pass whole-number bounds and expect a real number; the separate name keeps them on Random(double, double). * fix: RandomInt draws from the local engine, as up to 26.05.1 from one of its own From the review of #416:ee3af0bfmoved RandomInt from an engine of its own (std::mt19937 seeded by std::random_device) to Global.random_engine, the engine of the run's seed, and only the name was taken back here. Its one caller draws the trivia of the loading screen, in init_modes, before a network client gets the seed of the session in server_hello: the server's engine was then one draw ahead of the client's, and with a fixed randomseed the trivia was the same on every start. It is drawn from Global.local_random_engine now, the engine of what one machine shows, seeded from a true random source in init_modes before the loader is made. The test of the same seed checks the shared engine with Random() and the local one with RandomInt(); a new test checks that RandomInt() doesn't move the engine of the run's seed. --------- Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
@@ -88,7 +88,7 @@ std::vector<std::string> scenarioloader_ui::get_random_trivia()
|
|||||||
json triviaData = json::parse(fileContent);
|
json triviaData = json::parse(fileContent);
|
||||||
|
|
||||||
// select random
|
// select random
|
||||||
int i = Random(0, static_cast<int>(triviaData.size()) - 1);
|
int i = RandomInt(0, static_cast<int>(triviaData.size()) - 1);
|
||||||
std::string triviaStr = triviaData[i]["text"];
|
std::string triviaStr = triviaData[i]["text"];
|
||||||
std::string background = triviaData[i]["background"];
|
std::string background = triviaData[i]["background"];
|
||||||
if (triviaData[i].contains("scenery"))
|
if (triviaData[i].contains("scenery"))
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
#include "utilities/Globals.h"
|
#include "utilities/Globals.h"
|
||||||
#include "utilities/utilities.h"
|
#include "utilities/utilities.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
// the helpers of utilities/utilities.cpp and utilities.h as they behave now: the numbers and texts they give, their
|
// the helpers of utilities/utilities.cpp and utilities.h as they behave now: the numbers and texts they give, their
|
||||||
// oddities included. the ones which look like mistakes are listed in COVERAGE.md. the helpers which read from a
|
// oddities included. the ones which look like mistakes are listed in COVERAGE.md. the helpers which read from a
|
||||||
// parser (LoadPoint, the sets, the map) are in test_parser.cpp
|
// parser (LoadPoint, the sets, the map) are in test_parser.cpp
|
||||||
@@ -115,16 +118,31 @@ TEST_CASE("utilities: clearing a flag needs all of its bits set") {
|
|||||||
TEST_CASE("utilities: random whole numbers are within the bounds, both included, given in any order") {
|
TEST_CASE("utilities: random whole numbers are within the bounds, both included, given in any order") {
|
||||||
std::set<int> seen;
|
std::set<int> seen;
|
||||||
for (auto draw{0}; draw < 200; ++draw) {
|
for (auto draw{0}; draw < 200; ++draw) {
|
||||||
auto const value{Random(3, 7)};
|
auto const value{RandomInt(3, 7)};
|
||||||
CHECK(value >= 3);
|
CHECK(value >= 3);
|
||||||
CHECK(value <= 7);
|
CHECK(value <= 7);
|
||||||
seen.insert(value);
|
seen.insert(value);
|
||||||
auto const swapped{Random(7, 3)};
|
auto const swapped{RandomInt(7, 3)};
|
||||||
CHECK(swapped >= 3);
|
CHECK(swapped >= 3);
|
||||||
CHECK(swapped <= 7);
|
CHECK(swapped <= 7);
|
||||||
}
|
}
|
||||||
CHECK(seen.size() == 5);
|
CHECK(seen.size() == 5);
|
||||||
CHECK(Random(5, 5) == 5);
|
CHECK(RandomInt(5, 5) == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("utilities: whole-number bounds still give a real number in between") {
|
||||||
|
// the wind, the wheel flats, the truck hunting and the tachometer draw with whole-number bounds
|
||||||
|
static_assert(std::is_same_v<decltype(Random(0, 4)), double>);
|
||||||
|
auto fractions{0};
|
||||||
|
for (auto draw{0}; draw < 50; ++draw) {
|
||||||
|
auto const value{Random(0, 4)};
|
||||||
|
CHECK(value >= 0.0);
|
||||||
|
CHECK(value <= 4.0);
|
||||||
|
if (value != std::floor(value)) {
|
||||||
|
++fractions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CHECK(fractions > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("utilities: random numbers are within the bounds given in any order") {
|
TEST_CASE("utilities: random numbers are within the bounds given in any order") {
|
||||||
@@ -157,17 +175,37 @@ TEST_CASE("utilities: random numbers are within the bounds given in any order")
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("utilities: the same seed gives the same random numbers") {
|
TEST_CASE("utilities: the same seed gives the same random numbers") {
|
||||||
std::vector<int> first;
|
std::vector<double> first;
|
||||||
Global.random_engine.seed(42);
|
Global.random_engine.seed(42);
|
||||||
for (auto draw{0}; draw < 5; ++draw) {
|
for (auto draw{0}; draw < 5; ++draw) {
|
||||||
first.push_back(Random(0, 1000000));
|
first.push_back(Random(0.0, 1000000.0));
|
||||||
}
|
}
|
||||||
std::vector<int> second;
|
std::vector<double> second;
|
||||||
Global.random_engine.seed(42);
|
Global.random_engine.seed(42);
|
||||||
for (auto draw{0}; draw < 5; ++draw) {
|
for (auto draw{0}; draw < 5; ++draw) {
|
||||||
second.push_back(Random(0, 1000000));
|
second.push_back(Random(0.0, 1000000.0));
|
||||||
}
|
}
|
||||||
CHECK(first == second);
|
CHECK(first == second);
|
||||||
|
std::vector<int> localfirst;
|
||||||
|
Global.local_random_engine.seed(42);
|
||||||
|
for (auto draw{0}; draw < 5; ++draw) {
|
||||||
|
localfirst.push_back(RandomInt(0, 1000000));
|
||||||
|
}
|
||||||
|
std::vector<int> localsecond;
|
||||||
|
Global.local_random_engine.seed(42);
|
||||||
|
for (auto draw{0}; draw < 5; ++draw) {
|
||||||
|
localsecond.push_back(RandomInt(0, 1000000));
|
||||||
|
}
|
||||||
|
CHECK(localfirst == localsecond);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("utilities: the whole numbers are drawn from the local engine, not from that of the run's seed") {
|
||||||
|
// the trivia of the loading screen is drawn before a network client gets the seed of the session
|
||||||
|
Global.random_engine.seed(5);
|
||||||
|
auto const first{Random()};
|
||||||
|
Global.random_engine.seed(5);
|
||||||
|
RandomInt(0, 9);
|
||||||
|
CHECK(Random() == first);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("utilities: the local random numbers have an engine of their own") {
|
TEST_CASE("utilities: the local random numbers have an engine of their own") {
|
||||||
|
|||||||
@@ -150,11 +150,11 @@ double Random(double min, double max)
|
|||||||
return dist(Global.random_engine);
|
return dist(Global.random_engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Random(int min, int max)
|
int RandomInt(int min, int max)
|
||||||
{
|
{
|
||||||
if (max < min) { std::swap(min, max); } // std::uniform_int_distribution requires min <= max (inverted bounds are UB)
|
if (max < min) { std::swap(min, max); } // std::uniform_int_distribution requires min <= max (inverted bounds are UB)
|
||||||
std::uniform_int_distribution dist(min, max);
|
std::uniform_int_distribution dist(min, max);
|
||||||
return dist(Global.random_engine);
|
return dist(Global.local_random_engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint64_t true_random_seed()
|
std::uint64_t true_random_seed()
|
||||||
|
|||||||
@@ -64,7 +64,12 @@ inline long Round(double const f)
|
|||||||
}
|
}
|
||||||
|
|
||||||
double Random(double a, double b);
|
double Random(double a, double b);
|
||||||
int Random(int min, int max);
|
// whole numbers from min to max, both included. a name of its own keeps the calls of Random() with whole-number
|
||||||
|
// bounds (the wind, the wheel flats, the truck hunting, the tachometer) on Random(double, double), which draws the
|
||||||
|
// real number in between that they expect. drawn from the local engine, as up to 26.05.1 from an engine of its own: it
|
||||||
|
// picks what one machine shows (the trivia of the loading screen), and a draw from the engine of the run's seed would
|
||||||
|
// put that engine out of step with the other machines of a network session
|
||||||
|
int RandomInt(int min, int max);
|
||||||
|
|
||||||
/// @author lxvia
|
/// @author lxvia
|
||||||
/// @brief Returns a random seed from the system's entropy source, if available. Otherwise, falls back to a clock-based seed.
|
/// @brief Returns a random seed from the system's entropy source, if available. Otherwise, falls back to a clock-based seed.
|
||||||
|
|||||||
Reference in New Issue
Block a user