-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomV2.hpp
More file actions
127 lines (115 loc) · 2.86 KB
/
RandomV2.hpp
File metadata and controls
127 lines (115 loc) · 2.86 KB
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
#ifndef _RANDOM_H_
#define _RANDOM_H_
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
class Random
{
protected:
double number;
public:
Random()
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_real_distribution<> dist(0, 1);
number = dist(gen);
}
/// randomize
static Random* randomize()
{
return new Random();
}
/// returns 0-1 random double
inline double random();
/// returns 0-1 random int
inline int random_int();
/// returns random bool
inline bool random_bool(
unsigned int chance = 50
);
/// returns min-max random double
inline double random_range(
double min,
double max
);
/// returns min-max random int
inline int random_range_int(
int min,
int max
);
/// returns random char in argument `chars`
inline char random_char(
const char* chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
);
/// returns random std::string in argument `string` with length `length`
/*inline std::string random_string(
int length = 1,
std::string string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
);*/
/// returns random value from `arr`
inline std::string choice(
std::vector<std::string> arr
);
/// returns random `array` from `arr` with length `length`
inline std::vector<std::string> choices(
int length,
std::vector<std::string> arr
);
};
double Random::random()
{
return this->number;
}
int Random::random_int()
{
return (int)(this->number * 2);
}
bool Random::random_bool(unsigned int chance)
{
if (chance != 50)
{
if (this->random_range(0, 100) >= chance) {
return true;
} else {
return false;
}
} else {
return (bool)(this->random_int());
}
}
double Random::random_range(double min, double max)
{
if (min > max)
{
throw std::invalid_argument("argument \"min\" more than argument \"min\"");
}
return -(min - (this->random() * (max - min)));
}
int Random::random_range_int(int min, int max)
{
if (min > max)
{
throw std::invalid_argument("argument \"min\" more than argument \"min\"");
}
return -(int)(min - (this->random() * (max - min)));
}
char Random::random_char(const char* chars)
{
return chars[this->random_range_int(0, strlen(chars) - 1)];
}
std::string Random::choice(std::vector<std::string> arr)
{
return arr[this->random_range_int(0, arr.size())];
}
std::vector<std::string> Random::choices(int length, std::vector<std::string> arr)
{
std::vector<std::string> return_vector = {};
for (int i=0; i < length; i++)
{
return_vector.push_back(this->randomize()->choice(arr));
}
return return_vector;
}
#endif