/Users/deen/code/yugabyte-db/src/yb/util/random_util.cc
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 | | #include "yb/util/random_util.h" |
34 | | |
35 | | #include <cmath> |
36 | | #include <cstdlib> |
37 | | #include <random> |
38 | | |
39 | | #include "yb/util/random.h" |
40 | | |
41 | | namespace yb { |
42 | | |
43 | | namespace { |
44 | | |
45 | | const std::string kHumanReadableCharacters = |
46 | | "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
47 | | |
48 | | } |
49 | | |
50 | 321 | void RandomString(void* dest, size_t n, Random* rng) { |
51 | 321 | size_t i = 0; |
52 | 321 | uint32_t random = rng->Next(); |
53 | 321 | char* cdest = static_cast<char*>(dest); |
54 | 321 | static const size_t sz = sizeof(random); |
55 | 321 | if (n >= sz) { |
56 | 144M | for (i = 0; i <= n - sz; i += sz144M ) { |
57 | 144M | memcpy(&cdest[i], &random, sizeof(random)); |
58 | 144M | random = rng->Next(); |
59 | 144M | } |
60 | 307 | } |
61 | 321 | memcpy(cdest + i, &random, n - i); |
62 | 321 | } |
63 | | |
64 | 3.03M | uint32_t GetRandomSeed32() { |
65 | 3.03M | std::random_device device; |
66 | 3.03M | return device(); |
67 | 3.03M | } |
68 | | |
69 | 20 | std::vector<uint8_t> RandomBytes(size_t len, std::mt19937_64* rng) { |
70 | 20 | std::vector<uint8_t> data(len); |
71 | 14.5k | std::generate(data.begin(), data.end(), [=] { return RandomUniformInt(0, UCHAR_MAX, rng); }); |
72 | 20 | return data; |
73 | 20 | } |
74 | | |
75 | 474k | std::string RandomString(size_t len, std::mt19937_64* rng) { |
76 | 474k | std::string str; |
77 | 474k | str.reserve(len); |
78 | 204M | while (len > 0) { |
79 | 204M | str += yb::RandomUniformInt<char>(); |
80 | 204M | len--; |
81 | 204M | } |
82 | 474k | return str; |
83 | 474k | } |
84 | | |
85 | 5 | std::string RandomHumanReadableString(size_t len, Random* rnd) { |
86 | | // TODO: https://yugabyte.atlassian.net/browse/ENG-1508: Avoid code duplication in yb::Random and |
87 | | // rocksdb::Random. Currently this does not allow to reuse the same function in both code bases. |
88 | 5 | std::string ret; |
89 | 5 | ret.resize(len); |
90 | 230k | for (size_t i = 0; i < len; ++i230k ) { |
91 | 230k | ret[i] = static_cast<char>('a' + rnd->Uniform(26)); |
92 | 230k | } |
93 | 5 | return ret; |
94 | 5 | } |
95 | | |
96 | 45.6k | std::string RandomHumanReadableString(size_t len, std::mt19937_64* rng) { |
97 | 45.6k | std::string ret(len, 0); |
98 | 239M | for (size_t i = 0; i != len; ++i239M ) { |
99 | 239M | ret[i] = RandomElement(kHumanReadableCharacters, rng); |
100 | 239M | } |
101 | 45.6k | return ret; |
102 | 45.6k | } |
103 | | |
104 | | namespace { |
105 | | thread_local std::unique_ptr<std::mt19937_64> thread_local_random_ptr; |
106 | | } |
107 | | |
108 | 510M | std::mt19937_64& ThreadLocalRandom() { |
109 | 510M | auto* result = thread_local_random_ptr.get(); |
110 | 516M | if (result510M ) { |
111 | 516M | return *result; |
112 | 516M | } |
113 | 18.4E | thread_local_random_ptr.reset(result = new std::mt19937_64); |
114 | 18.4E | Seed(result); |
115 | 18.4E | return *result; |
116 | 510M | } |
117 | | |
118 | 697k | bool RandomUniformBool(std::mt19937_64* rng) { |
119 | 697k | return RandomUniformInt(0, 1, rng) != 0; |
120 | 697k | } |
121 | | |
122 | | } // namespace yb |