-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_help.hpp
More file actions
81 lines (65 loc) · 1.56 KB
/
random_help.hpp
File metadata and controls
81 lines (65 loc) · 1.56 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _RANDOM_HELP_RANDOM_HELP_HPP
#define _RANDOM_HELP_RANDOM_HELP_HPP
#include <ctime>
#include <functional>
#include <random>
namespace random_help
{
class rand_int_simple
{
public:
rand_int_simple(int lo, int hi) :
r(std::bind(std::uniform_int_distribution<>(lo, hi), std::default_random_engine()))
{
}
int operator()() const
{
return r();
}
private:
std::function<int()> r;
};
class rand_int
{
public:
rand_int(int lo, int hi) :
r(std::bind(std::uniform_int_distribution<>(lo, hi), std::default_random_engine()))
{
}
rand_int(int lo, int hi, bool seed_with_time, int seed = 0)
{
std::default_random_engine eng;
if(seed_with_time) eng.seed(std::default_random_engine::result_type(std::time(nullptr)));
else eng.seed(seed);
r = std::bind(std::uniform_int_distribution<>(lo, hi), eng);
}
int operator()() const
{
return r();
}
private:
std::function<int()> r;
};
class rand_real
{
public:
rand_real(double lo, double hi) :
r(std::bind(std::uniform_real_distribution<>(lo, hi), std::default_random_engine()))
{
}
rand_real(double lo, double hi, bool seed_with_time, int seed = 0)
{
std::default_random_engine eng;
if(seed_with_time) eng.seed(std::default_random_engine::result_type(std::time(nullptr)));
else eng.seed(seed);
r = std::bind(std::uniform_real_distribution<>(lo, hi), eng);
}
double operator()() const
{
return r();
}
private:
std::function<double()> r;
};
}
#endif