|
||||
File indexing completed on 2025-01-18 09:27:22
0001 // Copyright 2017 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // File: random.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // This header defines the recommended Uniform Random Bit Generator (URBG) 0020 // types for use within the Abseil Random library. These types are not 0021 // suitable for security-related use-cases, but should suffice for most other 0022 // uses of generating random values. 0023 // 0024 // The Abseil random library provides the following URBG types: 0025 // 0026 // * BitGen, a good general-purpose bit generator, optimized for generating 0027 // random (but not cryptographically secure) values 0028 // * InsecureBitGen, a slightly faster, though less random, bit generator, for 0029 // cases where the existing BitGen is a drag on performance. 0030 0031 #ifndef ABSL_RANDOM_RANDOM_H_ 0032 #define ABSL_RANDOM_RANDOM_H_ 0033 0034 #include <random> 0035 0036 #include "absl/random/distributions.h" // IWYU pragma: export 0037 #include "absl/random/internal/nonsecure_base.h" // IWYU pragma: export 0038 #include "absl/random/internal/pcg_engine.h" // IWYU pragma: export 0039 #include "absl/random/internal/pool_urbg.h" 0040 #include "absl/random/internal/randen_engine.h" 0041 #include "absl/random/seed_sequences.h" // IWYU pragma: export 0042 0043 namespace absl { 0044 ABSL_NAMESPACE_BEGIN 0045 0046 // ----------------------------------------------------------------------------- 0047 // absl::BitGen 0048 // ----------------------------------------------------------------------------- 0049 // 0050 // `absl::BitGen` is a general-purpose random bit generator for generating 0051 // random values for use within the Abseil random library. Typically, you use a 0052 // bit generator in combination with a distribution to provide random values. 0053 // 0054 // Example: 0055 // 0056 // // Create an absl::BitGen. There is no need to seed this bit generator. 0057 // absl::BitGen gen; 0058 // 0059 // // Generate an integer value in the closed interval [1,6] 0060 // int die_roll = absl::uniform_int_distribution<int>(1, 6)(gen); 0061 // 0062 // `absl::BitGen` is seeded by default with non-deterministic data to produce 0063 // different sequences of random values across different instances, including 0064 // different binary invocations. This behavior is different than the standard 0065 // library bit generators, which use golden values as their seeds. Default 0066 // construction intentionally provides no stability guarantees, to avoid 0067 // accidental dependence on such a property. 0068 // 0069 // `absl::BitGen` may be constructed with an optional seed sequence type, 0070 // conforming to [rand.req.seed_seq], which will be mixed with additional 0071 // non-deterministic data as detailed below. 0072 // 0073 // Example: 0074 // 0075 // // Create an absl::BitGen using an std::seed_seq seed sequence 0076 // std::seed_seq seq{1,2,3}; 0077 // absl::BitGen gen_with_seed(seq); 0078 // 0079 // // Generate an integer value in the closed interval [1,6] 0080 // int die_roll2 = absl::uniform_int_distribution<int>(1, 6)(gen_with_seed); 0081 // 0082 // Constructing two `absl::BitGen`s with the same seed sequence in the same 0083 // process will produce the same sequence of variates, but need not do so across 0084 // multiple processes even if they're executing the same binary. 0085 // 0086 // `absl::BitGen` meets the requirements of the Uniform Random Bit Generator 0087 // (URBG) concept as per the C++17 standard [rand.req.urng] though differs 0088 // slightly with [rand.req.eng]. Like its standard library equivalents (e.g. 0089 // `std::mersenne_twister_engine`) `absl::BitGen` is not cryptographically 0090 // secure. 0091 // 0092 // This type has been optimized to perform better than Mersenne Twister 0093 // (https://en.wikipedia.org/wiki/Mersenne_Twister) and many other complex URBG 0094 // types on modern x86, ARM, and PPC architectures. 0095 // 0096 // This type is thread-compatible, but not thread-safe. 0097 0098 // --------------------------------------------------------------------------- 0099 // absl::BitGen member functions 0100 // --------------------------------------------------------------------------- 0101 0102 // absl::BitGen::operator()() 0103 // 0104 // Calls the BitGen, returning a generated value. 0105 0106 // absl::BitGen::min() 0107 // 0108 // Returns the smallest possible value from this bit generator. 0109 0110 // absl::BitGen::max() 0111 // 0112 // Returns the largest possible value from this bit generator. 0113 0114 // absl::BitGen::discard(num) 0115 // 0116 // Advances the internal state of this bit generator by `num` times, and 0117 // discards the intermediate results. 0118 // --------------------------------------------------------------------------- 0119 0120 using BitGen = random_internal::NonsecureURBGBase< 0121 random_internal::randen_engine<uint64_t>>; 0122 0123 // ----------------------------------------------------------------------------- 0124 // absl::InsecureBitGen 0125 // ----------------------------------------------------------------------------- 0126 // 0127 // `absl::InsecureBitGen` is an efficient random bit generator for generating 0128 // random values, recommended only for performance-sensitive use cases where 0129 // `absl::BitGen` is not satisfactory when compute-bounded by bit generation 0130 // costs. 0131 // 0132 // Example: 0133 // 0134 // // Create an absl::InsecureBitGen 0135 // absl::InsecureBitGen gen; 0136 // for (size_t i = 0; i < 1000000; i++) { 0137 // 0138 // // Generate a bunch of random values from some complex distribution 0139 // auto my_rnd = some_distribution(gen, 1, 1000); 0140 // } 0141 // 0142 // Like `absl::BitGen`, `absl::InsecureBitGen` is seeded by default with 0143 // non-deterministic data to produce different sequences of random values across 0144 // different instances, including different binary invocations. (This behavior 0145 // is different than the standard library bit generators, which use golden 0146 // values as their seeds.) 0147 // 0148 // `absl::InsecureBitGen` may be constructed with an optional seed sequence 0149 // type, conforming to [rand.req.seed_seq], which will be mixed with additional 0150 // non-deterministic data, as detailed in the `absl::BitGen` comment. 0151 // 0152 // `absl::InsecureBitGen` meets the requirements of the Uniform Random Bit 0153 // Generator (URBG) concept as per the C++17 standard [rand.req.urng] though 0154 // its implementation differs slightly with [rand.req.eng]. Like its standard 0155 // library equivalents (e.g. `std::mersenne_twister_engine`) 0156 // `absl::InsecureBitGen` is not cryptographically secure. 0157 // 0158 // Prefer `absl::BitGen` over `absl::InsecureBitGen` as the general type is 0159 // often fast enough for the vast majority of applications. 0160 0161 using InsecureBitGen = 0162 random_internal::NonsecureURBGBase<random_internal::pcg64_2018_engine>; 0163 0164 // --------------------------------------------------------------------------- 0165 // absl::InsecureBitGen member functions 0166 // --------------------------------------------------------------------------- 0167 0168 // absl::InsecureBitGen::operator()() 0169 // 0170 // Calls the InsecureBitGen, returning a generated value. 0171 0172 // absl::InsecureBitGen::min() 0173 // 0174 // Returns the smallest possible value from this bit generator. 0175 0176 // absl::InsecureBitGen::max() 0177 // 0178 // Returns the largest possible value from this bit generator. 0179 0180 // absl::InsecureBitGen::discard(num) 0181 // 0182 // Advances the internal state of this bit generator by `num` times, and 0183 // discards the intermediate results. 0184 // --------------------------------------------------------------------------- 0185 0186 ABSL_NAMESPACE_END 0187 } // namespace absl 0188 0189 #endif // ABSL_RANDOM_RANDOM_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |