mirror of
https://gitlab.com/niansa/commoncpp.git
synced 2025-03-06 20:48:30 +01:00
Added RandomGenerator class
This commit is contained in:
parent
229b821232
commit
d0a9401a54
2 changed files with 49 additions and 1 deletions
|
@ -11,7 +11,8 @@ add_library(commoncpp STATIC
|
|||
config.cpp include/commoncpp/config.hpp
|
||||
utils.cpp include/commoncpp/utils.hpp
|
||||
pooled_thread.cpp include/commoncpp/pooled_thread.hpp
|
||||
include/commoncpp/timer.hpp)
|
||||
include/commoncpp/timer.hpp
|
||||
include/commoncpp/random.hpp)
|
||||
|
||||
target_include_directories(commoncpp PUBLIC include/)
|
||||
target_include_directories(commoncpp PRIVATE include/commoncpp/)
|
||||
|
|
47
include/commoncpp/random.hpp
Normal file
47
include/commoncpp/random.hpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#ifndef RANDOM_HPP
|
||||
#define RANDOM_HPP
|
||||
#include <random>
|
||||
|
||||
|
||||
|
||||
namespace common {
|
||||
class RandomGenerator {
|
||||
std::mt19937 rng;
|
||||
uint32_t initialSeed;
|
||||
|
||||
public:
|
||||
#ifndef COMMONCPP_LITE
|
||||
void seed() {
|
||||
rng.seed(initialSeed = std::random_device{}());
|
||||
}
|
||||
#endif
|
||||
void seed(uint32_t customSeed) {
|
||||
rng.seed(initialSeed = customSeed);
|
||||
}
|
||||
|
||||
unsigned getUInt() {
|
||||
std::uniform_int_distribution<unsigned> dist;
|
||||
return dist(rng);
|
||||
}
|
||||
unsigned getUInt(unsigned max) {
|
||||
std::uniform_int_distribution<unsigned> dist(0, max);
|
||||
return dist(rng);
|
||||
}
|
||||
unsigned getUInt(unsigned min, unsigned max) {
|
||||
std::uniform_int_distribution<unsigned> dist(min, max);
|
||||
return dist(rng);
|
||||
}
|
||||
double getDouble(double max) {
|
||||
std::uniform_real_distribution<double> dist(0.0, max);
|
||||
return dist(rng);
|
||||
}
|
||||
double getDouble(double min, double max) {
|
||||
std::uniform_real_distribution<double> dist(min, max);
|
||||
return dist(rng);
|
||||
}
|
||||
bool getBool(float chance) {
|
||||
return getDouble(1.0) <= chance && chance != 0.0f;
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // RANDOM_HPP
|
Loading…
Add table
Reference in a new issue