-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRandomGenerator.cpp
70 lines (52 loc) · 1.58 KB
/
RandomGenerator.cpp
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
//-----------------------------------------------------------------------------
/** @file libboardgame_base/RandomGenerator.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "RandomGenerator.h"
#include <list>
namespace libboardgame_base {
//-----------------------------------------------------------------------------
namespace {
bool is_seed_set = false;
RandomGenerator::ResultType the_seed;
list<RandomGenerator*>& get_all_generators()
{
static list<RandomGenerator*> all_generators;
return all_generators;
}
RandomGenerator::ResultType get_nondet_seed()
{
random_device generator;
return generator();
}
} // namespace
//-----------------------------------------------------------------------------
RandomGenerator::RandomGenerator()
{
set_seed(is_seed_set ? the_seed : get_nondet_seed());
get_all_generators().push_back(this);
}
RandomGenerator::~RandomGenerator()
{
get_all_generators().remove(this);
}
bool RandomGenerator::has_global_seed()
{
return is_seed_set;
}
void RandomGenerator::set_global_seed(ResultType seed)
{
is_seed_set = true;
the_seed = seed;
for (RandomGenerator* i : get_all_generators())
i->set_seed(the_seed);
}
void RandomGenerator::set_global_seed_last()
{
if (is_seed_set)
for (RandomGenerator* i : get_all_generators())
i->set_seed(the_seed);
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base