-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrng.h
43 lines (33 loc) · 1.17 KB
/
rng.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#include <random>
#include <array>
#include <algorithm>
#include <functional>
#include <cmath>
class RandomEngine {
std::mt19937_64 generator;
public:
RandomEngine();
RandomEngine(const RandomEngine &) = delete;
RandomEngine& operator=(const RandomEngine &) = delete;
std::mt19937_64& gen() {
return generator;
}
static RandomEngine& getThreadLocalEngein();
};
class RNG {
public:
RNG() = default;
template <typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
T operator()(const T begin, const T end) {
return std::uniform_int_distribution<T>{begin, end}(RandomEngine::getThreadLocalEngein().gen());
}
template <typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
T operator()(const T begin, const T end) {
return std::uniform_real_distribution<T>{begin, end}(RandomEngine::getThreadLocalEngein().gen());
}
template <typename RandomAccessIterator>
void shuffle(RandomAccessIterator first, RandomAccessIterator last) {
std::shuffle(first, last, RandomEngine::getThreadLocalEngein().gen());
}
};