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:
@@ -150,11 +150,11 @@ double Random(double min, double max)
|
||||
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)
|
||||
std::uniform_int_distribution dist(min, max);
|
||||
return dist(Global.random_engine);
|
||||
return dist(Global.local_random_engine);
|
||||
}
|
||||
|
||||
std::uint64_t true_random_seed()
|
||||
|
||||
@@ -64,7 +64,12 @@ inline long Round(double const f)
|
||||
}
|
||||
|
||||
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
|
||||
/// @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