/
random_util.hpp
75 lines (59 loc) · 1.35 KB
/
random_util.hpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Sample provided by Fabio Galuppo
//October 2016
//http://www.simplycpp.com
#ifndef RANDOM_UTIL_HPP
#define RANDOM_UTIL_HPP
#include <random>
namespace internals
{
using random_engine = std::default_random_engine;
static thread_local random_engine current_engine;
inline random_engine::result_type rd()
{
return std::random_device{}();
}
inline random_engine& rand_engine()
{
static thread_local random_engine engine{ rd() };
return engine;
}
inline void seed_rand()
{
rand_engine().seed(rd());
}
inline void seed_rand(random_engine::result_type seed)
{
rand_engine().seed(seed);
}
struct rand_int_func
{
using int_distribution = std::uniform_int_distribution<int>;
int_distribution distribution;
int operator()()
{
return distribution(rand_engine());
}
int operator()(int min_inclusive, int max_inclusive)
{
return distribution(rand_engine(), int_distribution::param_type{ min_inclusive, max_inclusive });
}
};
static thread_local rand_int_func rand_int;
}
inline void seed_rand()
{
internals::seed_rand();
}
inline void seed_rand(internals::random_engine::result_type seed)
{
internals::seed_rand(seed);
}
inline int rand_int()
{
return internals::rand_int();
}
inline int rand_int(int min_inclusive, int max_inclusive)
{
return internals::rand_int(min_inclusive, max_inclusive);
}
#endif /* RANDOM_UTIL_HPP */