random.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: random.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header defines the recommended Uniform Random Bit Generator (URBG)
  20. // types for use within the Abseil Random library. These types are not
  21. // suitable for security-related use-cases, but should suffice for most other
  22. // uses of generating random values.
  23. //
  24. // The Abseil random library provides the following URBG types:
  25. //
  26. // * BitGen, a good general-purpose bit generator, optimized for generating
  27. // random (but not cryptographically secure) values
  28. // * InsecureBitGen, a slightly faster, though less random, bit generator, for
  29. // cases where the existing BitGen is a drag on performance.
  30. #ifndef Y_ABSL_RANDOM_RANDOM_H_
  31. #define Y_ABSL_RANDOM_RANDOM_H_
  32. #include <random>
  33. #include "y_absl/random/distributions.h" // IWYU pragma: export
  34. #include "y_absl/random/internal/nonsecure_base.h" // IWYU pragma: export
  35. #include "y_absl/random/internal/pcg_engine.h" // IWYU pragma: export
  36. #include "y_absl/random/internal/pool_urbg.h"
  37. #include "y_absl/random/internal/randen_engine.h"
  38. #include "y_absl/random/seed_sequences.h" // IWYU pragma: export
  39. namespace y_absl {
  40. Y_ABSL_NAMESPACE_BEGIN
  41. // -----------------------------------------------------------------------------
  42. // y_absl::BitGen
  43. // -----------------------------------------------------------------------------
  44. //
  45. // `y_absl::BitGen` is a general-purpose random bit generator for generating
  46. // random values for use within the Abseil random library. Typically, you use a
  47. // bit generator in combination with a distribution to provide random values.
  48. //
  49. // Example:
  50. //
  51. // // Create an y_absl::BitGen. There is no need to seed this bit generator.
  52. // y_absl::BitGen gen;
  53. //
  54. // // Generate an integer value in the closed interval [1,6]
  55. // int die_roll = y_absl::uniform_int_distribution<int>(1, 6)(gen);
  56. //
  57. // `y_absl::BitGen` is seeded by default with non-deterministic data to produce
  58. // different sequences of random values across different instances, including
  59. // different binary invocations. This behavior is different than the standard
  60. // library bit generators, which use golden values as their seeds. Default
  61. // construction intentionally provides no stability guarantees, to avoid
  62. // accidental dependence on such a property.
  63. //
  64. // `y_absl::BitGen` may be constructed with an optional seed sequence type,
  65. // conforming to [rand.req.seed_seq], which will be mixed with additional
  66. // non-deterministic data as detailed below.
  67. //
  68. // Example:
  69. //
  70. // // Create an y_absl::BitGen using an std::seed_seq seed sequence
  71. // std::seed_seq seq{1,2,3};
  72. // y_absl::BitGen gen_with_seed(seq);
  73. //
  74. // // Generate an integer value in the closed interval [1,6]
  75. // int die_roll2 = y_absl::uniform_int_distribution<int>(1, 6)(gen_with_seed);
  76. //
  77. // Constructing two `y_absl::BitGen`s with the same seed sequence in the same
  78. // process will produce the same sequence of variates, but need not do so across
  79. // multiple processes even if they're executing the same binary.
  80. //
  81. // `y_absl::BitGen` meets the requirements of the Uniform Random Bit Generator
  82. // (URBG) concept as per the C++17 standard [rand.req.urng] though differs
  83. // slightly with [rand.req.eng]. Like its standard library equivalents (e.g.
  84. // `std::mersenne_twister_engine`) `y_absl::BitGen` is not cryptographically
  85. // secure.
  86. //
  87. // This type has been optimized to perform better than Mersenne Twister
  88. // (https://en.wikipedia.org/wiki/Mersenne_Twister) and many other complex URBG
  89. // types on modern x86, ARM, and PPC architectures.
  90. //
  91. // This type is thread-compatible, but not thread-safe.
  92. // ---------------------------------------------------------------------------
  93. // y_absl::BitGen member functions
  94. // ---------------------------------------------------------------------------
  95. // y_absl::BitGen::operator()()
  96. //
  97. // Calls the BitGen, returning a generated value.
  98. // y_absl::BitGen::min()
  99. //
  100. // Returns the smallest possible value from this bit generator.
  101. // y_absl::BitGen::max()
  102. //
  103. // Returns the largest possible value from this bit generator.
  104. // y_absl::BitGen::discard(num)
  105. //
  106. // Advances the internal state of this bit generator by `num` times, and
  107. // discards the intermediate results.
  108. // ---------------------------------------------------------------------------
  109. using BitGen = random_internal::NonsecureURBGBase<
  110. random_internal::randen_engine<uint64_t>>;
  111. // -----------------------------------------------------------------------------
  112. // y_absl::InsecureBitGen
  113. // -----------------------------------------------------------------------------
  114. //
  115. // `y_absl::InsecureBitGen` is an efficient random bit generator for generating
  116. // random values, recommended only for performance-sensitive use cases where
  117. // `y_absl::BitGen` is not satisfactory when compute-bounded by bit generation
  118. // costs.
  119. //
  120. // Example:
  121. //
  122. // // Create an y_absl::InsecureBitGen
  123. // y_absl::InsecureBitGen gen;
  124. // for (size_t i = 0; i < 1000000; i++) {
  125. //
  126. // // Generate a bunch of random values from some complex distribution
  127. // auto my_rnd = some_distribution(gen, 1, 1000);
  128. // }
  129. //
  130. // Like `y_absl::BitGen`, `y_absl::InsecureBitGen` is seeded by default with
  131. // non-deterministic data to produce different sequences of random values across
  132. // different instances, including different binary invocations. (This behavior
  133. // is different than the standard library bit generators, which use golden
  134. // values as their seeds.)
  135. //
  136. // `y_absl::InsecureBitGen` may be constructed with an optional seed sequence
  137. // type, conforming to [rand.req.seed_seq], which will be mixed with additional
  138. // non-deterministic data, as detailed in the `y_absl::BitGen` comment.
  139. //
  140. // `y_absl::InsecureBitGen` meets the requirements of the Uniform Random Bit
  141. // Generator (URBG) concept as per the C++17 standard [rand.req.urng] though
  142. // its implementation differs slightly with [rand.req.eng]. Like its standard
  143. // library equivalents (e.g. `std::mersenne_twister_engine`)
  144. // `y_absl::InsecureBitGen` is not cryptographically secure.
  145. //
  146. // Prefer `y_absl::BitGen` over `y_absl::InsecureBitGen` as the general type is
  147. // often fast enough for the vast majority of applications.
  148. using InsecureBitGen =
  149. random_internal::NonsecureURBGBase<random_internal::pcg64_2018_engine>;
  150. // ---------------------------------------------------------------------------
  151. // y_absl::InsecureBitGen member functions
  152. // ---------------------------------------------------------------------------
  153. // y_absl::InsecureBitGen::operator()()
  154. //
  155. // Calls the InsecureBitGen, returning a generated value.
  156. // y_absl::InsecureBitGen::min()
  157. //
  158. // Returns the smallest possible value from this bit generator.
  159. // y_absl::InsecureBitGen::max()
  160. //
  161. // Returns the largest possible value from this bit generator.
  162. // y_absl::InsecureBitGen::discard(num)
  163. //
  164. // Advances the internal state of this bit generator by `num` times, and
  165. // discards the intermediate results.
  166. // ---------------------------------------------------------------------------
  167. Y_ABSL_NAMESPACE_END
  168. } // namespace y_absl
  169. #endif // Y_ABSL_RANDOM_RANDOM_H_