MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
random.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_HELPER_STRING_RANDOM_H_
27#define ROUTER_SRC_HELPER_STRING_RANDOM_H_
28
29#include <algorithm>
30#include <random>
31
32namespace helper {
33
34/**
35 * Base class for generators.
36 *
37 * Generator must provide only following function:
38 *
39 * static char generate()
40 *
41 * and doesn't need to inherit from `GeneratorBase`.
42 */
44 /**
45 * Static method that generates random number.
46 *
47 * This method was introduced for easier changing the algorithm
48 * in future. `range` argument decides about the generated numbers
49 * range [0, range).
50 */
51 static int get_random_int(int range) {
52 std::random_device rd; // a seed source for the random number engine
53 std::uniform_int_distribution<> distrib(0, range - 1);
54 return distrib(rd);
55 }
56};
57
59 protected:
60 const static char smallEnd = 'z';
61 const static char smallBegin = 'a';
62 const static char bigEnd = 'Z';
63 const static char bigBegin = 'A';
64
65 const static int smallRange = (smallEnd - smallBegin) + 1;
66 const static int bigRange = (bigEnd - bigBegin) + 1;
67
68 public:
69 const static int kNumberOfCharacters = smallRange;
70 static char generate() {
71 std::random_device rd; // a seed source for the random number engine
72 std::uniform_int_distribution<> distrib(smallBegin, smallEnd);
73 return distrib(rd);
74 }
75};
76
78 public:
80
81 static char generate() {
83 if (result < smallRange) return smallBegin + result;
85 return bigBegin + result;
86 }
87};
88
90 public:
91 const static char numericEnd = '9';
92 const static char numericBegin = '0';
93
94 const static int numericRange = (numericEnd - numericBegin) + 1;
96
97 static char generate() {
99 if (result < smallRange) return smallBegin + result;
101
102 if (result < bigRange) return bigBegin + result;
103 result -= bigRange;
104
105 return numericBegin + result;
106 }
107};
108
110 public:
111 static char generate() {
112 auto result = get_random_int(255);
113
114 return static_cast<char>(result);
115 }
116};
117
118template <typename Generator = GeneratorSmallAlpha>
119inline std::string generate_string(uint32_t length) {
120 std::string result(length, '0');
121 std::generate(result.begin(), result.end(), &Generator::generate);
122
123 return result;
124}
125
126template <uint32_t length, typename Generator = GeneratorSmallAlpha>
127inline std::string generate_string() {
128 return generate_string<Generator>(length);
129}
130
131} // namespace helper
132
133#endif // ROUTER_SRC_HELPER_STRING_RANDOM_H_
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: cache.h:33
std::string generate_string(uint32_t length)
Definition: random.h:119
struct result result
Definition: result.h:34
Definition: random.h:109
static char generate()
Definition: random.h:111
Definition: random.h:89
static char generate()
Definition: random.h:97
static const int numericRange
Definition: random.h:94
static const char numericEnd
Definition: random.h:91
static const int kNumberOfCharacters
Definition: random.h:95
static const char numericBegin
Definition: random.h:92
Definition: random.h:77
static char generate()
Definition: random.h:81
static const int kNumberOfCharacters
Definition: random.h:79
Base class for generators.
Definition: random.h:43
static int get_random_int(int range)
Static method that generates random number.
Definition: random.h:51
Definition: random.h:58
static const int kNumberOfCharacters
Definition: random.h:69
static const char smallEnd
Definition: random.h:60
static const char bigEnd
Definition: random.h:62
static char generate()
Definition: random.h:70
static const char bigBegin
Definition: random.h:63
static const int bigRange
Definition: random.h:66
static const int smallRange
Definition: random.h:65
static const char smallBegin
Definition: random.h:61
Definition: gen_lex_token.cc:149
Definition: result.h:30