first commit

This commit is contained in:
Bob Polis
2021-10-06 10:55:22 +02:00
commit 087e31219c
6 changed files with 228 additions and 0 deletions

23
src/random.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "random.hpp"
sc::random sc::random::singleton {};
bool sc::random::random_bool() {
std::uniform_int_distribution<int> dist {0, 1};
return dist(_reng) == 1;
}
int sc::random::random_int(int from, int to) {
std::uniform_int_distribution<int> dist {from, to};
return dist(_reng);
}
double sc::random::random_double() {
std::uniform_real_distribution<double> dist {};
return dist(_reng);
}
double sc::random::random_double(double from, double to) {
std::uniform_real_distribution<double> dist {from, to};
return dist(_reng);
}

45
src/random.hpp Normal file
View File

@ -0,0 +1,45 @@
#ifndef RANDOM_H_
#define RANDOM_H_
#include <random>
namespace sc {
class random {
public:
static random& instance() { return singleton; }
// forbid copying
random(const random&) = delete;
random& operator=(const random&) = delete;
// forbid moving
random(random&&) = delete;
random& operator=(random&&) = delete;
std::default_random_engine& engine() { return _reng; }
bool random_bool();
int random_int(int from, int to);
double random_double();
double random_double(double from, double to);
template <typename T, typename Iter>
T choice(Iter beg, Iter end) {
std::uniform_int_distribution<int> dist {0, end - beg - 1};
return beg + dist(_reng);
}
private:
static random singleton;
random() = default;
~random() = default;
std::random_device _rdev {};
std::default_random_engine _reng {_rdev()};
};
}
#endif // RANDOM_H_