-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRandomize.h
201 lines (162 loc) · 4.61 KB
/
Randomize.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef CPP_RANDOMIZE_H
#define CPP_RANDOMIZE_H
#include <random>
#include <algorithm>
namespace Randomize {
using std::string;
using std::mt19937;
template<class T>
class Randomizer {
public:
virtual T create() = 0;
};
class Integer : public Randomizer<int> {
private:
int _min;
int _max;
mt19937 rng;
public:
Integer(int min = 1, int max = 9) {
_min = min;
_max = max;
rng.seed(std::random_device()());
}
int create() {
std::uniform_int_distribution<int> dist6(_min, _max);
return dist6(rng);
}
};
class Double : public Randomizer<double> {
private:
double _min;
double _max;
mt19937 rng;
public:
Double(double min = 0, double max = 1) {
_min = min;
_max = max;
rng.seed(std::random_device()());
}
double create() {
std::uniform_real_distribution<double> dist6(_min, _max);
return dist6(rng);
}
};
class String : public Randomizer<string> {
private:
int _length;
string _letters;
public:
String(int length = 16, const string &letters = "abcdefghijklmnopqrstuvwxyz") {
_length = length;
_letters = letters;
}
string create() {
string text = "";
Integer randomizer(0, _letters.length() - 1);
for (int i = 0; i < _length; i++) {
text += _letters[randomizer.create()];
}
return text;
}
};
template<class T>
class Array2D : public Randomizer<void> {
private:
int _N;
int _M;
Randomizer<T> *_randomizer;
bool _sorted;
public:
Array2D(int N = 10, int M = 10, Randomizer<T> &randomizer = *(new Integer())) {
_N = N;
_M = M;
_randomizer = &randomizer;
_sorted = false;
}
Array2D sorted(bool sorted = true) {
_sorted = sorted;
return *this;
}
void create() {
throw std::invalid_argument("Call 'void fill(T *D)' instead.");
}
void fill(T *D) {
for (int i = 0; i < _N; i++) {
for (int j = 0; j < _M; j++) {
D[i * _M + j] = (*_randomizer).create();
}
if (_sorted) std::sort(&D[i * _M], &D[i * _M] + _M);
}
}
};
template<class T>
class Array1D : public Randomizer<void> {
private:
int _N;
Randomizer<T> *_randomizer;
bool _sorted;
public:
Array1D(int N = 10, Randomizer<T> &randomizer = *(new Integer())) {
_N = N;
_randomizer = &randomizer;
_sorted = false;
}
Array1D sorted(bool sorted = true) {
_sorted = sorted;
return *this;
}
void create() {
throw std::invalid_argument("Call 'void fill(T *D)' instead.");
}
void fill(T *D) {
for (int i = 0; i < _N; i++) {
D[i] = (*_randomizer).create();
}
if (_sorted) std::sort(D, D + _N);
}
};
template<class T>
class Graph : public Randomizer<void> {
private:
int _N;
double _ratio;
Randomizer<T> *_randomizer;
bool _directed;
bool _weighted;
public:
Graph(int N = 5, double ratio = .3, Randomizer<T> &randomizer = *(new Integer())) {
_N = N;
_ratio = ratio;
_randomizer = &randomizer;
_directed = true;
_weighted = false;
}
Graph directed(bool directed = true) {
_directed = directed;
return *this;
}
Graph weighted(bool weighted = true) {
_weighted = weighted;
return *this;
}
void create() {
throw std::invalid_argument("Call 'void fill(T *G)' instead.");
}
void fill(T *G) {
Double ratioRandomizer;
for (int i = 0; i < _N; i++) {
for (int j = 0; j < _N; j++) {
if (i == j) {
G[i * _N + j] = 0;
} else if (_directed || i < j) {
G[i * _N + j] = ratioRandomizer.create() < _ratio ? _weighted ? (*_randomizer).create() : 1 : 0;
} else {
G[i * _N + j] = G[j * _N + i];
}
}
}
}
};
}
#endif