/Users/deen/code/yugabyte-db/src/yb/util/random_util.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #ifndef YB_UTIL_RANDOM_UTIL_H |
34 | | #define YB_UTIL_RANDOM_UTIL_H |
35 | | |
36 | | #include <algorithm> |
37 | | #include <random> |
38 | | |
39 | | #include <glog/logging.h> // For CHECK |
40 | | |
41 | | namespace yb { |
42 | | |
43 | | class Random; |
44 | | |
45 | | // Writes exactly n random bytes to dest using the parameter Random generator. |
46 | | // Note RandomString() does not null-terminate its strings, though '\0' could |
47 | | // be written to dest with the same probability as any other byte. |
48 | | void RandomString(void* dest, size_t n, Random* rng); |
49 | | |
50 | | // Generate a 32-bit random seed from several sources, including timestamp, |
51 | | // pid & tid. |
52 | | uint32_t GetRandomSeed32(); |
53 | | |
54 | | std::vector<uint8_t> RandomBytes(size_t len, std::mt19937_64* rng = nullptr); |
55 | | std::string RandomString(size_t len, std::mt19937_64* rng = nullptr); |
56 | | |
57 | | std::string RandomHumanReadableString(size_t len, Random* rnd); |
58 | | |
59 | | class RandomDeviceSequence { |
60 | | public: |
61 | | typedef std::random_device::result_type result_type; |
62 | | |
63 | | template<class It> |
64 | 182k | void generate(It begin, const It& end) { |
65 | 114M | std::generate(begin, end, [this] { return device_(); }); |
66 | 182k | } |
67 | | private: |
68 | | std::random_device device_; |
69 | | }; |
70 | | |
71 | | // Correct seeding of random number generator. |
72 | | // It is quite futile to use 32bit seed for generator with 19968bit state, like mt19937. |
73 | | template<class Engine> |
74 | 183k | void Seed(Engine* engine) { |
75 | 183k | RandomDeviceSequence sequence; |
76 | 183k | engine->seed(sequence); |
77 | 183k | } |
78 | | |
79 | | std::mt19937_64& ThreadLocalRandom(); |
80 | | |
81 | | template <class Int> |
82 | 505M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { |
83 | 505M | if (!rng) { |
84 | 500M | rng = &ThreadLocalRandom(); |
85 | 500M | } |
86 | 505M | return std::uniform_int_distribution<Int>(min, max)(*rng); |
87 | 505M | } int yb::RandomUniformInt<int>(int, int, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 29.9M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 29.9M | if (!rng) { | 84 | 24.7M | rng = &ThreadLocalRandom(); | 85 | 24.7M | } | 86 | 29.9M | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 29.9M | } |
unsigned long long yb::RandomUniformInt<unsigned long long>(unsigned long long, unsigned long long, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 9.58M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 9.58M | if (!rng9.58M ) { | 84 | 9.58M | rng = &ThreadLocalRandom(); | 85 | 9.58M | } | 86 | 9.58M | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 9.58M | } |
long long yb::RandomUniformInt<long long>(long long, long long, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 301k | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 301k | if (!rng) { | 84 | 2.01k | rng = &ThreadLocalRandom(); | 85 | 2.01k | } | 86 | 301k | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 301k | } |
unsigned long yb::RandomUniformInt<unsigned long>(unsigned long, unsigned long, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 247M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 247M | if (!rng) { | 84 | 246M | rng = &ThreadLocalRandom(); | 85 | 246M | } | 86 | 247M | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 247M | } |
unsigned int yb::RandomUniformInt<unsigned int>(unsigned int, unsigned int, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 351k | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 351k | if (!rng) { | 84 | 351k | rng = &ThreadLocalRandom(); | 85 | 351k | } | 86 | 351k | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 351k | } |
unsigned short yb::RandomUniformInt<unsigned short>(unsigned short, unsigned short, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 8.71M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 8.72M | if (!rng8.71M ) { | 84 | 8.72M | rng = &ThreadLocalRandom(); | 85 | 8.72M | } | 86 | 8.71M | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 8.71M | } |
long yb::RandomUniformInt<long>(long, long, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 39.7k | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 39.7k | if (!rng) { | 84 | 39.7k | rng = &ThreadLocalRandom(); | 85 | 39.7k | } | 86 | 39.7k | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 39.7k | } |
char yb::RandomUniformInt<char>(char, char, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 82 | 209M | Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) { | 83 | 210M | if (!rng209M ) { | 84 | 210M | rng = &ThreadLocalRandom(); | 85 | 210M | } | 86 | 209M | return std::uniform_int_distribution<Int>(min, max)(*rng); | 87 | 209M | } |
|
88 | | |
89 | | bool RandomUniformBool(std::mt19937_64* rng = nullptr); |
90 | | |
91 | | template <class Int> |
92 | | std::vector<Int> RandomUniformVector(Int min, Int max, uint32_t size, |
93 | 10 | std::mt19937_64* rng = nullptr) { |
94 | 10 | std::vector<Int> vec(size); |
95 | 100 | std::generate(vec.begin(), vec.end(), [=] { return RandomUniformInt(min, max, rng); }); |
96 | 10 | return vec; |
97 | 10 | } |
98 | | |
99 | | template <class Int> |
100 | | bool RandomWithChance(Int chance, std::mt19937_64* rng = nullptr) { |
101 | | if (!rng) { |
102 | | rng = &ThreadLocalRandom(); |
103 | | } |
104 | | return RandomUniformInt(static_cast<Int>(0), chance - 1, rng) == 0; |
105 | | } |
106 | | |
107 | | template <class Int> |
108 | 233M | Int RandomUniformInt(std::mt19937_64* rng = nullptr) { |
109 | 233M | typedef std::numeric_limits<Int> Limits; |
110 | 233M | return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng); |
111 | 233M | } unsigned long long yb::RandomUniformInt<unsigned long long>(std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 108 | 9.35M | Int RandomUniformInt(std::mt19937_64* rng = nullptr) { | 109 | 9.35M | typedef std::numeric_limits<Int> Limits; | 110 | 9.35M | return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng); | 111 | 9.35M | } |
int yb::RandomUniformInt<int>(std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 108 | 3.44M | Int RandomUniformInt(std::mt19937_64* rng = nullptr) { | 109 | 3.44M | typedef std::numeric_limits<Int> Limits; | 110 | 3.44M | return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng); | 111 | 3.44M | } |
unsigned int yb::RandomUniformInt<unsigned int>(std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 108 | 1.73k | Int RandomUniformInt(std::mt19937_64* rng = nullptr) { | 109 | 1.73k | typedef std::numeric_limits<Int> Limits; | 110 | 1.73k | return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng); | 111 | 1.73k | } |
char yb::RandomUniformInt<char>(std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 108 | 220M | Int RandomUniformInt(std::mt19937_64* rng = nullptr) { | 109 | 220M | typedef std::numeric_limits<Int> Limits; | 110 | 220M | return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng); | 111 | 220M | } |
|
112 | | |
113 | | template <class Real> |
114 | 10.5M | Real RandomUniformReal(Real a, Real b, std::mt19937_64* rng = nullptr) { |
115 | 10.5M | if (!rng10.5M ) { |
116 | 10.5M | rng = &ThreadLocalRandom(); |
117 | 10.5M | } |
118 | 10.5M | return std::uniform_real_distribution<Real>(a, b)(*rng); |
119 | 10.5M | } |
120 | | |
121 | | template <class Real> |
122 | 1.35M | Real RandomUniformReal(std::mt19937_64* rng = nullptr) { |
123 | 1.35M | return RandomUniformReal(static_cast<Real>(0), static_cast<Real>(1), rng); |
124 | 1.35M | } |
125 | | |
126 | 57.4M | inline bool RandomActWithProbability(double probability, std::mt19937_64* rng = nullptr) { |
127 | 57.4M | return probability <= 0 ? false56.3M |
128 | 57.4M | : probability >= 1.01.14M ? true19 |
129 | 1.14M | : RandomUniformReal<double>(rng) < probability1.14M ; |
130 | 57.4M | } |
131 | | |
132 | | template <class Collection> |
133 | | typename Collection::const_reference RandomElement(const Collection& collection, |
134 | 240M | std::mt19937_64* rng = nullptr) { |
135 | 240M | CHECK(!collection.empty()); |
136 | 240M | size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng); |
137 | 240M | auto it = collection.begin(); |
138 | 240M | std::advance(it, index); |
139 | 240M | return *it; |
140 | 240M | } std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::const_reference yb::RandomElement<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 134 | 76.0k | std::mt19937_64* rng = nullptr) { | 135 | 76.0k | CHECK(!collection.empty()); | 136 | 76.0k | size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng); | 137 | 76.0k | auto it = collection.begin(); | 138 | 76.0k | std::advance(it, index); | 139 | 76.0k | return *it; | 140 | 76.0k | } |
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> >::const_reference yb::RandomElement<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*> > const&, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 134 | 399k | std::mt19937_64* rng = nullptr) { | 135 | 399k | CHECK(!collection.empty()); | 136 | 399k | size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng); | 137 | 399k | auto it = collection.begin(); | 138 | 399k | std::advance(it, index); | 139 | 399k | return *it; | 140 | 399k | } |
Unexecuted instantiation: std::__1::vector<std::__1::shared_ptr<yb::tablet::TabletPeer>, std::__1::allocator<std::__1::shared_ptr<yb::tablet::TabletPeer> > >::const_reference yb::RandomElement<std::__1::vector<std::__1::shared_ptr<yb::tablet::TabletPeer>, std::__1::allocator<std::__1::shared_ptr<yb::tablet::TabletPeer> > > >(std::__1::vector<std::__1::shared_ptr<yb::tablet::TabletPeer>, std::__1::allocator<std::__1::shared_ptr<yb::tablet::TabletPeer> > > const&, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) std::__1::vector<std::__1::shared_ptr<yb::tserver::MiniTabletServer>, std::__1::allocator<std::__1::shared_ptr<yb::tserver::MiniTabletServer> > >::const_reference yb::RandomElement<std::__1::vector<std::__1::shared_ptr<yb::tserver::MiniTabletServer>, std::__1::allocator<std::__1::shared_ptr<yb::tserver::MiniTabletServer> > > >(std::__1::vector<std::__1::shared_ptr<yb::tserver::MiniTabletServer>, std::__1::allocator<std::__1::shared_ptr<yb::tserver::MiniTabletServer> > > const&, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 134 | 4 | std::mt19937_64* rng = nullptr) { | 135 | 4 | CHECK(!collection.empty()); | 136 | 4 | size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng); | 137 | 4 | auto it = collection.begin(); | 138 | 4 | std::advance(it, index); | 139 | 4 | return *it; | 140 | 4 | } |
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::const_reference yb::RandomElement<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::mersenne_twister_engine<unsigned long long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ull, 29ul, 6148914691236517205ull, 17ul, 8202884508482404352ull, 37ul, 18444473444759240704ull, 43ul, 6364136223846793005ull>*) Line | Count | Source | 134 | 239M | std::mt19937_64* rng = nullptr) { | 135 | 239M | CHECK(!collection.empty()); | 136 | 239M | size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng); | 137 | 239M | auto it = collection.begin(); | 138 | 239M | std::advance(it, index); | 139 | 239M | return *it; | 140 | 239M | } |
|
141 | | |
142 | | std::string RandomHumanReadableString(size_t len, std::mt19937_64* rng = nullptr); |
143 | | |
144 | | } // namespace yb |
145 | | |
146 | | #endif // YB_UTIL_RANDOM_UTIL_H |