YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/random_util.h
Line
Count
Source (jump to first uncovered line)
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
130k
  void generate(It begin, const It& end) {
65
81.5M
    std::generate(begin, end, [this] { return device_(); });
66
130k
  }
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
130k
void Seed(Engine* engine) {
75
130k
  RandomDeviceSequence sequence;
76
130k
  engine->seed(sequence);
77
130k
}
_ZN2yb4SeedINSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEEEEvPT_
Line
Count
Source
74
130k
void Seed(Engine* engine) {
75
130k
  RandomDeviceSequence sequence;
76
130k
  engine->seed(sequence);
77
130k
}
_ZN2yb4SeedINSt3__123mersenne_twister_engineIjLm32ELm624ELm397ELm31ELj2567483615ELm11ELj4294967295ELm7ELj2636928640ELm15ELj4022730752ELm18ELj1812433253EEEEEvPT_
Line
Count
Source
74
32
void Seed(Engine* engine) {
75
32
  RandomDeviceSequence sequence;
76
32
  engine->seed(sequence);
77
32
}
78
79
std::mt19937_64& ThreadLocalRandom();
80
81
template <class Int>
82
475M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
475M
  if (!rng) {
84
471M
    rng = &ThreadLocalRandom();
85
471M
  }
86
475M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
475M
}
_ZN2yb16RandomUniformIntIiEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
14.1M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
14.1M
  if (!rng) {
84
9.44M
    rng = &ThreadLocalRandom();
85
9.44M
  }
86
14.1M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
14.1M
}
_ZN2yb16RandomUniformIntItEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
9.21M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
9.21M
  if (!rng) {
84
9.21M
    rng = &ThreadLocalRandom();
85
9.21M
  }
86
9.21M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
9.21M
}
_ZN2yb16RandomUniformIntImEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
239M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
239M
  if (!rng) {
84
238M
    rng = &ThreadLocalRandom();
85
238M
  }
86
239M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
239M
}
_ZN2yb16RandomUniformIntIxEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
302k
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
302k
  if (!rng) {
84
2.81k
    rng = &ThreadLocalRandom();
85
2.81k
  }
86
302k
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
302k
}
_ZN2yb16RandomUniformIntIyEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
4.92M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
4.92M
  if (!rng) {
84
4.92M
    rng = &ThreadLocalRandom();
85
4.92M
  }
86
4.92M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
4.92M
}
_ZN2yb16RandomUniformIntIjEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
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
}
_ZN2yb16RandomUniformIntIcEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
207M
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
208M
  if (!rng) {
84
208M
    rng = &ThreadLocalRandom();
85
208M
  }
86
207M
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
207M
}
_ZN2yb16RandomUniformIntIlEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
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
}
_ZN2yb16RandomUniformIntIhEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
82
5.49k
Int RandomUniformInt(Int min, Int max, std::mt19937_64* rng = nullptr) {
83
5.49k
  if (!rng) {
84
5.49k
    rng = &ThreadLocalRandom();
85
5.49k
  }
86
5.49k
  return std::uniform_int_distribution<Int>(min, max)(*rng);
87
5.49k
}
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
4
                                     std::mt19937_64* rng = nullptr) {
94
4
  std::vector<Int> vec(size);
95
40
  std::generate(vec.begin(), vec.end(), [=] { return RandomUniformInt(min, max, rng); });
96
4
  return vec;
97
4
}
98
99
template <class Int>
100
0
bool RandomWithChance(Int chance, std::mt19937_64* rng = nullptr) {
101
0
  if (!rng) {
102
0
    rng = &ThreadLocalRandom();
103
0
  }
104
0
  return RandomUniformInt(static_cast<Int>(0), chance - 1, rng) == 0;
105
0
}
106
107
template <class Int>
108
226M
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
226M
  typedef std::numeric_limits<Int> Limits;
110
226M
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
226M
}
_ZN2yb16RandomUniformIntIyEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
4.80M
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
4.80M
  typedef std::numeric_limits<Int> Limits;
110
4.80M
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
4.80M
}
_ZN2yb16RandomUniformIntIjEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
1.61k
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
1.61k
  typedef std::numeric_limits<Int> Limits;
110
1.61k
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
1.61k
}
_ZN2yb16RandomUniformIntIiEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
2.95M
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
2.95M
  typedef std::numeric_limits<Int> Limits;
110
2.95M
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
2.95M
}
_ZN2yb16RandomUniformIntItEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
9.20M
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
9.20M
  typedef std::numeric_limits<Int> Limits;
110
9.20M
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
9.20M
}
_ZN2yb16RandomUniformIntIcEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
209M
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
209M
  typedef std::numeric_limits<Int> Limits;
110
209M
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
209M
}
_ZN2yb16RandomUniformIntIxEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
108
14
Int RandomUniformInt(std::mt19937_64* rng = nullptr) {
109
14
  typedef std::numeric_limits<Int> Limits;
110
14
  return RandomUniformInt<Int>(Limits::min(), Limits::max(), rng);
111
14
}
112
113
template <class Real>
114
6.20M
Real RandomUniformReal(Real a, Real b, std::mt19937_64* rng = nullptr) {
115
6.20M
  if (!rng) {
116
6.20M
    rng = &ThreadLocalRandom();
117
6.20M
  }
118
6.20M
  return std::uniform_real_distribution<Real>(a, b)(*rng);
119
6.20M
}
_ZN2yb17RandomUniformRealIdEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
114
6.20M
Real RandomUniformReal(Real a, Real b, std::mt19937_64* rng = nullptr) {
115
6.20M
  if (!rng) {
116
6.20M
    rng = &ThreadLocalRandom();
117
6.20M
  }
118
6.20M
  return std::uniform_real_distribution<Real>(a, b)(*rng);
119
6.20M
}
_ZN2yb17RandomUniformRealIfEET_S1_S1_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
114
1
Real RandomUniformReal(Real a, Real b, std::mt19937_64* rng = nullptr) {
115
1
  if (!rng) {
116
1
    rng = &ThreadLocalRandom();
117
1
  }
118
1
  return std::uniform_real_distribution<Real>(a, b)(*rng);
119
1
}
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
}
_ZN2yb17RandomUniformRealIdEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
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
}
_ZN2yb17RandomUniformRealIfEET_PNSt3__123mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
122
1
Real RandomUniformReal(std::mt19937_64* rng = nullptr) {
123
1
  return RandomUniformReal(static_cast<Real>(0), static_cast<Real>(1), rng);
124
1
}
125
126
18.5M
inline bool RandomActWithProbability(double probability, std::mt19937_64* rng = nullptr) {
127
17.3M
  return probability <= 0 ? false
128
1.15M
                          : probability >= 1.0 ? true
129
1.15M
                                               : RandomUniformReal<double>(rng) < probability;
130
18.5M
}
131
132
template <class Collection>
133
typename Collection::const_reference RandomElement(const Collection& collection,
134
235M
                                                   std::mt19937_64* rng = nullptr) {
135
235M
  CHECK(!collection.empty());
136
235M
  size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng);
137
235M
  auto it = collection.begin();
138
235M
  std::advance(it, index);
139
235M
  return *it;
140
235M
}
_ZN2yb13RandomElementINSt3__16vectorINS1_10unique_ptrINS_25BatchTimeseriesDataSourceENS1_14default_deleteIS4_EEEENS1_9allocatorIS7_EEEEEENT_15const_referenceERKSB_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
134
937k
                                                   std::mt19937_64* rng = nullptr) {
135
937k
  CHECK(!collection.empty());
136
937k
  size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng);
137
937k
  auto it = collection.begin();
138
937k
  std::advance(it, index);
139
937k
  return *it;
140
937k
}
Unexecuted instantiation: _ZN2yb13RandomElementINSt3__16vectorINS1_10shared_ptrINS_7tserver16MiniTabletServerEEENS1_9allocatorIS6_EEEEEENT_15const_referenceERKSA_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
_ZN2yb13RandomElementINSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEENT_15const_referenceERKS8_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
134
234M
                                                   std::mt19937_64* rng = nullptr) {
135
234M
  CHECK(!collection.empty());
136
234M
  size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng);
137
234M
  auto it = collection.begin();
138
234M
  std::advance(it, index);
139
234M
  return *it;
140
234M
}
Unexecuted instantiation: _ZN2yb13RandomElementINSt3__16vectorINS1_10shared_ptrINS_6tablet10TabletPeerEEENS1_9allocatorIS6_EEEEEENT_15const_referenceERKSA_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
_ZN2yb13RandomElementINSt3__16vectorINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS6_IS8_EEEEEENT_15const_referenceERKSB_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
134
61.3k
                                                   std::mt19937_64* rng = nullptr) {
135
61.3k
  CHECK(!collection.empty());
136
61.3k
  size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng);
137
61.3k
  auto it = collection.begin();
138
61.3k
  std::advance(it, index);
139
61.3k
  return *it;
140
61.3k
}
_ZN2yb13RandomElementINSt3__16vectorIPKNS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS6_ISA_EEEEEENT_15const_referenceERKSD_PNS1_23mersenne_twister_engineIyLm64ELm312ELm156ELm31ELy13043109905998158313ELm29ELy6148914691236517205ELm17ELy8202884508482404352ELm37ELy18444473444759240704ELm43ELy6364136223846793005EEE
Line
Count
Source
134
221k
                                                   std::mt19937_64* rng = nullptr) {
135
221k
  CHECK(!collection.empty());
136
221k
  size_t index = RandomUniformInt<size_t>(0, collection.size() - 1, rng);
137
221k
  auto it = collection.begin();
138
221k
  std::advance(it, index);
139
221k
  return *it;
140
221k
}
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