16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 00:49:19 +02:00

fix: guard Random/LocalRandom against inverted min>max bounds

std::uniform_real/int_distribution has UB when min>max. The old "fancy Random"
used interpolate(a,b,t) which tolerated a>b; the migration to
std::uniform_real_distribution did not, and only one inverted call site was
fixed by hand. Swap the bounds inside Random()/Random(int)/LocalRandom() so
every call site is safe, and reorder the two inverted literal calls in
Driver.cpp (fActionTime buzzer / departure delay) for readability.
This commit is contained in:
maj00r
2026-07-02 00:16:04 +02:00
parent 03bfbb1345
commit 5fce417a51
2 changed files with 5 additions and 2 deletions

View File

@@ -125,12 +125,14 @@ bool ClearFlag(int &Flag, int const Value)
double Random(double min, double max)
{
if (max < min) { std::swap(min, max); } // std::uniform_real_distribution requires min <= max (inverted bounds are UB)
std::uniform_real_distribution<double> dist(min, max);
return dist(Global.random_engine);
}
int Random(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<int> dist(min, max);
return dist(Global.random_engine);
}
@@ -163,6 +165,7 @@ std::string generate_uuid_v4()
double LocalRandom(double a, double b)
{
if (b < a) { std::swap(a, b); } // std::uniform_real_distribution requires a <= b (inverted bounds are UB)
std::uniform_real_distribution<double> dist(a, b);
return dist(Global.local_random_engine);
}