-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathRandom.cpp
135 lines (105 loc) · 2.84 KB
/
Random.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* File: Random.cpp
* Project: DUtils library
* Author: Dorian Galvez-Lopez
* Date: April 2010
* Description: manages pseudo-random numbers
* License: see the LICENSE.txt file
*
*/
#include "Random.h"
#include <cstdlib>
#include <chrono>
using namespace std;
namespace utils{
bool Random::m_already_seeded = false;
void Random::SeedRand() {
auto now = std::chrono::high_resolution_clock::now();
auto duration = now.time_since_epoch();
unsigned seed = static_cast<unsigned>(std::chrono::duration_cast<std::chrono::microseconds>(duration).count());
srand(seed);
}
void Random::SeedRandOnce()
{
if(!m_already_seeded)
{
Random::SeedRand();
m_already_seeded = true;
}
}
void Random::SeedRand(int seed)
{
srand(seed);
}
void Random::SeedRandOnce(int seed)
{
if(!m_already_seeded)
{
Random::SeedRand(seed);
m_already_seeded = true;
}
}
int Random::RandomInt(int min, int max){
int d = max - min + 1;
return int(((double)rand()/((double)RAND_MAX + 1.0)) * d) + min;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
Random::UnrepeatedRandomizer::UnrepeatedRandomizer(int min, int max)
{
if(min <= max)
{
m_min = min;
m_max = max;
}
else
{
m_min = max;
m_max = min;
}
createValues();
}
// ---------------------------------------------------------------------------
Random::UnrepeatedRandomizer::UnrepeatedRandomizer
(const Random::UnrepeatedRandomizer& rnd)
{
*this = rnd;
}
// ---------------------------------------------------------------------------
int Random::UnrepeatedRandomizer::get()
{
if(empty()) createValues();
Random::SeedRandOnce();
int k = Random::RandomInt(0, m_values.size()-1);
int ret = m_values[k];
m_values[k] = m_values.back();
m_values.pop_back();
return ret;
}
// ---------------------------------------------------------------------------
void Random::UnrepeatedRandomizer::createValues()
{
int n = m_max - m_min + 1;
m_values.resize(n);
for(int i = 0; i < n; ++i) m_values[i] = m_min + i;
}
// ---------------------------------------------------------------------------
void Random::UnrepeatedRandomizer::reset()
{
if((int)m_values.size() != m_max - m_min + 1) createValues();
}
// ---------------------------------------------------------------------------
Random::UnrepeatedRandomizer&
Random::UnrepeatedRandomizer::operator=
(const Random::UnrepeatedRandomizer& rnd)
{
if(this != &rnd)
{
this->m_min = rnd.m_min;
this->m_max = rnd.m_max;
this->m_values = rnd.m_values;
}
return *this;
}
// ---------------------------------------------------------------------------
}