-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlowRandom.h
98 lines (82 loc) · 3.22 KB
/
FlowRandom.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
#pragma once
#include <random>
#pragma warning(push)
#pragma warning(disable: 4244 4267)
namespace FlowRandom {
inline const double randomM1t1() {
static std::mt19937 generator(std::time(nullptr));
static std::uniform_real_distribution<double> distribution(-1.0, 1.0);
return distribution(generator);
}
inline const double random0t1() {
static std::mt19937 generator(std::time(nullptr));
static std::uniform_real_distribution<double> distribution(0, 1.0);
return distribution(generator);
}
inline double RandomAdd(const double weight, const double min, const double max) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_real_distribution<double> distribution(min, max);
return weight + distribution(generator);
}
inline double Random(const double min, const double max) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_real_distribution<double> distribution(min, max);
return distribution(generator);
}
inline int RandomInt(const int min, const int max) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_int_distribution<int> distribution(min, max);
return distribution(generator);
}
inline size_t RandomSizeT(const int max) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_int_distribution<size_t> distribution(0, max);
return distribution(generator);
}
inline unsigned char randomChar() {
return FlowRandom::RandomSizeT(255);
}
inline bool RandomTrue(double chance, double in) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_real_distribution<double> distribution(0, in);
return chance > distribution(generator);
}
inline bool RandomTrue(double chance) {
static std::mt19937 generator(std::time(nullptr));
std::uniform_real_distribution<double> distribution(0, 1);
return chance > distribution(generator);
}
inline double NormalDistribution(double mean = 0, double variation = 1) {
static std::mt19937 generator(std::time(nullptr));
std::normal_distribution<double> distribution(mean, variation);
return distribution(generator);
}
inline static const std::string
alphaNum =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
inline static std::vector<unsigned char> getRandomBytes(const size_t lenght = 32){
std::vector<unsigned char> bytes;
bytes.reserve(lenght);
for(size_t i = 0; i < lenght; ++i){
bytes.emplace_back(randomChar());
}
return bytes;
}
inline static std::string getRandomString(size_t len = 5, const std::string &charSet = "") {
std::string rtn;
rtn.reserve(5);
if (charSet.empty()) {
for (; len != 0; --len) {
rtn += alphaNum.at(RandomSizeT(alphaNum.length() - 1));
}
} else {
for (; len != 0; --len) {
rtn += charSet.at(RandomSizeT(charSet.length() - 1));
}
}
return rtn;
}
}
#pragma warning( pop )