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:
@@ -8,6 +8,9 @@
|
||||
#include "utilities/Globals.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
|
||||
// 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
|
||||
@@ -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") {
|
||||
std::set<int> seen;
|
||||
for (auto draw{0}; draw < 200; ++draw) {
|
||||
auto const value{Random(3, 7)};
|
||||
auto const value{RandomInt(3, 7)};
|
||||
CHECK(value >= 3);
|
||||
CHECK(value <= 7);
|
||||
seen.insert(value);
|
||||
auto const swapped{Random(7, 3)};
|
||||
auto const swapped{RandomInt(7, 3)};
|
||||
CHECK(swapped >= 3);
|
||||
CHECK(swapped <= 7);
|
||||
}
|
||||
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") {
|
||||
@@ -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") {
|
||||
std::vector<int> first;
|
||||
std::vector<double> first;
|
||||
Global.random_engine.seed(42);
|
||||
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);
|
||||
for (auto draw{0}; draw < 5; ++draw) {
|
||||
second.push_back(Random(0, 1000000));
|
||||
second.push_back(Random(0.0, 1000000.0));
|
||||
}
|
||||
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") {
|
||||
|
||||
Reference in New Issue
Block a user