mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-02 09:19:12 +01:00
Merge branch 'master' into feat/uart-2.0
This commit is contained in:
@@ -726,7 +726,9 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string&
|
||||
|
||||
if (token == "randomseed")
|
||||
{
|
||||
ParseOne(Parser, Global.random_seed, 1);
|
||||
std::string text;
|
||||
ParseOne(Parser, text);
|
||||
Global.random_seed = seed_of(text);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ Copyright (C) 2007-2014 Maciej Cierniak
|
||||
//
|
||||
//#include <sys/types.h>
|
||||
//#include <sys/stat.h>
|
||||
#include <charconv>
|
||||
#include <ranges>
|
||||
//#ifndef WIN32
|
||||
//#include <unistd.h>
|
||||
@@ -28,9 +29,9 @@ Copyright (C) 2007-2014 Maciej Cierniak
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/parser.h"
|
||||
#include "utilities/U8.h"
|
||||
#include "utilities/Logs.h"
|
||||
|
||||
|
||||
//#include "utilities/Logs.h"
|
||||
|
||||
// TODO: This shouldn't be in Globals?
|
||||
bool DebugModeFlag = false;
|
||||
@@ -137,15 +138,51 @@ int Random(int min, int max)
|
||||
return dist(Global.random_engine);
|
||||
}
|
||||
|
||||
std::uint64_t true_random_seed()
|
||||
{
|
||||
try
|
||||
{
|
||||
std::random_device rd;
|
||||
if (rd.entropy() > 0.0)
|
||||
{
|
||||
// NOTE: both halves have to be 64 bit wide before the shift - shifting a std::uint32_t
|
||||
// by 32 is undefined behaviour, and drops the high half of the seed
|
||||
std::uint64_t const high{rd()};
|
||||
std::uint64_t const low{rd()};
|
||||
return (high << 32) | low;
|
||||
}
|
||||
}
|
||||
catch (std::exception const &Error)
|
||||
{
|
||||
// std::random_device throws when the machine has no usable hardware entropy source;
|
||||
// the clock below stands in for it, which is worth saying out loud since it is a
|
||||
// weaker seed than the caller asked for
|
||||
WriteLog(std::format("random: no hardware entropy source ({}), seeding from the clock instead", Error.what()));
|
||||
}
|
||||
return static_cast<std::uint64_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
||||
}
|
||||
|
||||
std::uint32_t seed_of(std::string const &Text)
|
||||
{
|
||||
if (std::uint32_t number{}; std::from_chars(Text.data(), Text.data() + Text.size(), number).ec == std::errc{})
|
||||
{
|
||||
return number;
|
||||
}
|
||||
std::uint32_t hash{};
|
||||
for (auto const character : Text)
|
||||
{
|
||||
hash = hash * 31u + static_cast<std::uint32_t>(static_cast<unsigned char>(character));
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
std::string generate_uuid_v4()
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<int> dist(0, 255);
|
||||
|
||||
std::array<uint8_t, 16> bytes;
|
||||
for (auto &b : bytes)
|
||||
b = static_cast<uint8_t>(dist(gen));
|
||||
b = static_cast<uint8_t>(dist(Global.random_engine));
|
||||
|
||||
// UUID v4 (RFC 4122)
|
||||
bytes[6] = bytes[6] & 0x0F | 0x40;
|
||||
|
||||
@@ -62,6 +62,18 @@ inline long Round(double const f)
|
||||
|
||||
double Random(double a, double b);
|
||||
int Random(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.
|
||||
/// @return A random seed as 64 bit unsigned integer
|
||||
std::uint64_t true_random_seed();
|
||||
|
||||
// The seed a word stands for. A number is taken as it is written; anything else is
|
||||
// hashed the way Java's String.hashCode does it, so the same word gives the same
|
||||
// world on every platform and every build - which is the whole point of telling
|
||||
// somebody a seed.
|
||||
std::uint32_t seed_of(std::string const &Text);
|
||||
|
||||
std::string generate_uuid_v4();
|
||||
double LocalRandom(double a, double b);
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include "utilities/utilities.h"
|
||||
#include "utilities/Globals.h"
|
||||
|
||||
class UID {
|
||||
public:
|
||||
@@ -13,10 +15,9 @@ public:
|
||||
|
||||
|
||||
static UID random() {
|
||||
static thread_local std::mt19937_64 gen(std::random_device{}());
|
||||
UID u;
|
||||
uint64_t a = gen();
|
||||
uint64_t b = gen();
|
||||
uint64_t a = Global.random_engine();
|
||||
uint64_t b = Global.random_engine();
|
||||
for (int i = 0; i < 8; ++i) u.bytes[i] = uint8_t(a >> (i * 8) & 0xFF);
|
||||
for (int i = 0; i < 8; ++i) u.bytes[8 + i] = uint8_t(b >> (i * 8) & 0xFF);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user