first commit
This commit is contained in:
23
src/random.cpp
Normal file
23
src/random.cpp
Normal 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
45
src/random.hpp
Normal 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_
|
Reference in New Issue
Block a user