2018-05-11 16:10:36 -07:00
|
|
|
#include "helpers/random_helpers.h"
|
2016-07-17 06:22:05 -07:00
|
|
|
#include <string>
|
2017-06-21 17:26:52 -07:00
|
|
|
#include <vector>
|
2017-08-08 12:42:49 -07:00
|
|
|
#include <random>
|
|
|
|
|
#include <time.h>
|
2016-07-17 06:22:05 -07:00
|
|
|
|
|
|
|
|
using std::string;
|
2017-06-21 17:26:52 -07:00
|
|
|
using std::vector;
|
2016-07-17 06:22:05 -07:00
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
Generator default_generator(0);
|
2017-08-08 12:42:49 -07:00
|
|
|
|
|
|
|
|
unsigned get_time_as_seed() {
|
|
|
|
|
return time(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
void Generator::reseed(unsigned seed) {
|
2017-08-08 12:42:49 -07:00
|
|
|
engine.seed(seed);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
unsigned Generator::operator()() {
|
|
|
|
|
return distribution(engine);
|
2017-08-08 12:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
unsigned Generator::operator()(unsigned max) {
|
|
|
|
|
return distribution(engine) % max;
|
2017-08-08 12:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
string Generator::str(char min, char max) {
|
2016-07-17 06:22:05 -07:00
|
|
|
string result;
|
2018-05-11 16:10:36 -07:00
|
|
|
size_t length = operator()(12);
|
2016-07-17 06:22:05 -07:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2018-05-11 16:10:36 -07:00
|
|
|
result += (min + operator()(max - min));
|
2016-07-17 06:22:05 -07:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 17:08:46 -07:00
|
|
|
static string operator_characters = "!(){}[]<>+-=";
|
|
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
string Generator::words(size_t count) {
|
2016-07-17 06:22:05 -07:00
|
|
|
string result;
|
|
|
|
|
bool just_inserted_word = false;
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2018-05-11 16:10:36 -07:00
|
|
|
if (operator()(10) < 6) {
|
2018-05-11 17:08:46 -07:00
|
|
|
result += operator_characters[operator()(operator_characters.size())];
|
2016-07-17 06:22:05 -07:00
|
|
|
} else {
|
|
|
|
|
if (just_inserted_word)
|
|
|
|
|
result += " ";
|
2018-05-11 16:10:36 -07:00
|
|
|
result += str('a', 'z');
|
2016-07-17 06:22:05 -07:00
|
|
|
just_inserted_word = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-06-21 17:26:52 -07:00
|
|
|
|
2018-05-11 16:10:36 -07:00
|
|
|
string Generator::select(const vector<string> &list) {
|
|
|
|
|
return list[operator()(list.size())];
|
2017-06-21 17:26:52 -07:00
|
|
|
}
|
2018-05-11 17:08:46 -07:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
void Generator::sleep_some() {
|
|
|
|
|
Sleep(operator()(5));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
void Generator::sleep_some() {
|
|
|
|
|
usleep(operator()(5 * 1000));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|