-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.h
More file actions
56 lines (44 loc) · 1.05 KB
/
Copy pathrandom.h
File metadata and controls
56 lines (44 loc) · 1.05 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* random.h
*/
#ifndef BASE_RANDOM_H_
#define BASE_RANDOM_H_
#include <cstdint>
#include <random>
namespace ave {
namespace base {
class Random {
public:
explicit Random(uint64_t seed) : generator_(seed) {}
uint32_t Rand(uint32_t low, uint32_t high) {
std::uniform_int_distribution<uint32_t> dist(low, high);
return dist(generator_);
}
int Rand(int low, int high) {
std::uniform_int_distribution<int> dist(low, high);
return dist(generator_);
}
uint32_t Rand(uint32_t t) {
if (t == 0) {
return 0;
}
std::uniform_int_distribution<uint32_t> dist(0, t - 1);
return dist(generator_);
}
uint32_t Rand() { return generator_(); }
template <typename T>
T Rand() {
std::uniform_int_distribution<T> dist(0, std::numeric_limits<T>::max());
return dist(generator_);
}
private:
std::mt19937 generator_;
};
template <>
inline bool Random::Rand<bool>() {
std::uniform_int_distribution<int> dist(0, 1);
return dist(generator_) == 1;
}
} // namespace base
} // namespace ave
#endif // BASE_RANDOM_H_