first commit
This commit is contained in:
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