0.2.0
Loading...
Searching...
No Matches
RandomNumberGenerator.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <random>
17#include <string>
18
19#include <nlohmann/json.hpp>
20using json = nlohmann::json;
21
22#include "SHA256.hpp"
23
24namespace NAV
25{
26
29{
30 public:
32 explicit RandomNumberGenerator(bool useSeed = true) : useSeed(useSeed) {} // NOLINT(cert-msc32-c,cert-msc51-cpp)
43
46 void resetSeed(size_t id = 0)
47 {
48 uint64_t seed = useSeed ? this->seed : static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count());
49 auto hash = hashSeed(std::to_string(seed) + (useSeed || id == 0 ? "" : " " + std::to_string(id)));
50 std::seed_seq seed_seq(hash.begin(), hash.end());
51 _generator.seed(seed_seq);
52 }
53
56 void resetSeedOnce(uint64_t userSeed)
57 {
58 auto hash = hashSeed(std::to_string(userSeed));
59 std::seed_seq seed_seq(hash.begin(), hash.end());
60 _generator.seed(seed_seq);
61 }
62
68 template<typename IntType = int>
69 double getRand_uniformIntDist(IntType min = 0, IntType max = std::numeric_limits<IntType>::max())
70 {
71 return std::uniform_int_distribution<IntType>(min, max)(_generator);
72 }
73
79 template<typename RealType = double>
80 double getRand_uniformRealDist(RealType min = 0.0, RealType max = 1.0)
81 {
82 return std::uniform_real_distribution<RealType>(min, max)(_generator);
83 }
84
90 template<typename RealType = double>
91 double getRand_normalDist(RealType mean = 0.0, RealType stddev = 1.0)
92 {
93 return std::normal_distribution<RealType>(mean, stddev)(_generator);
94 }
95
96 bool useSeed = true;
97 uint64_t seed = 0;
98
99 private:
103 static std::string hashSeed(const std::string& seed)
104 {
105 SHA256 sha;
106 sha.update(seed);
107 uint8_t* digest = sha.digest();
108
109 std::string hashed(digest, digest + 32); // NOLINT //SHA256::ToString method is shady... (manipulates data?). Keep Pointer Arithmetic
110 delete[] digest; // NOLINT
111
112 return hashed;
113 }
114
115 std::mt19937_64 _generator;
116};
117
121void to_json(json& j, const RandomNumberGenerator& rng);
125void from_json(const json& j, RandomNumberGenerator& rng);
126
127} // namespace NAV
nlohmann::json json
json namespace
Definition FlowManager.hpp:21
Manages a thread which calls a specified function at a specified interval.
Definition RandomNumberGenerator.hpp:29
RandomNumberGenerator(bool useSeed=true)
Default constructor.
Definition RandomNumberGenerator.hpp:32
RandomNumberGenerator & operator=(const RandomNumberGenerator &)=delete
Copy assignment operator.
uint64_t seed
Seed for the random number generator.
Definition RandomNumberGenerator.hpp:97
double getRand_uniformIntDist(IntType min=0, IntType max=std::numeric_limits< IntType >::max())
Gets a random integer number from an uniform distribution.
Definition RandomNumberGenerator.hpp:69
bool useSeed
Flag whether to use the seed instead of the system time.
Definition RandomNumberGenerator.hpp:96
double getRand_uniformRealDist(RealType min=0.0, RealType max=1.0)
Gets a random real number from an uniform distribution.
Definition RandomNumberGenerator.hpp:80
double getRand_normalDist(RealType mean=0.0, RealType stddev=1.0)
Gets a random number from a normal distribution.
Definition RandomNumberGenerator.hpp:91
~RandomNumberGenerator()=default
Destructor.
RandomNumberGenerator(const RandomNumberGenerator &)=delete
Copy constructor.
void resetSeed(size_t id=0)
Reset the seed to the internal seed or the system time.
Definition RandomNumberGenerator.hpp:46
RandomNumberGenerator(RandomNumberGenerator &&)=delete
Move constructor.
RandomNumberGenerator & operator=(RandomNumberGenerator &&)=delete
Move assignment operator.
void resetSeedOnce(uint64_t userSeed)
Reset the seed to the specified seed, but do not update the internal seed.
Definition RandomNumberGenerator.hpp:56