seed_sequences.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: seed_sequences.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header contains utilities for creating and working with seed sequences
  20. // conforming to [rand.req.seedseq]. In general, direct construction of seed
  21. // sequences is discouraged, but use-cases for construction of identical bit
  22. // generators (using the same seed sequence) may be helpful (e.g. replaying a
  23. // simulation whose state is derived from variates of a bit generator).
  24. #ifndef Y_ABSL_RANDOM_SEED_SEQUENCES_H_
  25. #define Y_ABSL_RANDOM_SEED_SEQUENCES_H_
  26. #include <iterator>
  27. #include <random>
  28. #include "y_absl/base/config.h"
  29. #include "y_absl/random/internal/salted_seed_seq.h"
  30. #include "y_absl/random/internal/seed_material.h"
  31. #include "y_absl/random/seed_gen_exception.h"
  32. #include "y_absl/types/span.h"
  33. namespace y_absl {
  34. Y_ABSL_NAMESPACE_BEGIN
  35. // -----------------------------------------------------------------------------
  36. // y_absl::SeedSeq
  37. // -----------------------------------------------------------------------------
  38. //
  39. // `y_absl::SeedSeq` constructs a seed sequence according to [rand.req.seedseq]
  40. // for use within bit generators. `y_absl::SeedSeq`, unlike `std::seed_seq`
  41. // additionally salts the generated seeds with extra implementation-defined
  42. // entropy. For that reason, you can use `y_absl::SeedSeq` in combination with
  43. // standard library bit generators (e.g. `std::mt19937`) to introduce
  44. // non-determinism in your seeds.
  45. //
  46. // Example:
  47. //
  48. // y_absl::SeedSeq my_seed_seq({a, b, c});
  49. // std::mt19937 my_bitgen(my_seed_seq);
  50. //
  51. using SeedSeq = random_internal::SaltedSeedSeq<std::seed_seq>;
  52. // -----------------------------------------------------------------------------
  53. // y_absl::CreateSeedSeqFrom(bitgen*)
  54. // -----------------------------------------------------------------------------
  55. //
  56. // Constructs a seed sequence conforming to [rand.req.seedseq] using variates
  57. // produced by a provided bit generator.
  58. //
  59. // You should generally avoid direct construction of seed sequences, but
  60. // use-cases for reuse of a seed sequence to construct identical bit generators
  61. // may be helpful (eg. replaying a simulation whose state is derived from bit
  62. // generator values).
  63. //
  64. // If bitgen == nullptr, then behavior is undefined.
  65. //
  66. // Example:
  67. //
  68. // y_absl::BitGen my_bitgen;
  69. // auto seed_seq = y_absl::CreateSeedSeqFrom(&my_bitgen);
  70. // y_absl::BitGen new_engine(seed_seq); // derived from my_bitgen, but not
  71. // // correlated.
  72. //
  73. template <typename URBG>
  74. SeedSeq CreateSeedSeqFrom(URBG* urbg) {
  75. SeedSeq::result_type
  76. seed_material[random_internal::kEntropyBlocksNeeded];
  77. if (!random_internal::ReadSeedMaterialFromURBG(
  78. urbg, y_absl::MakeSpan(seed_material))) {
  79. random_internal::ThrowSeedGenException();
  80. }
  81. return SeedSeq(std::begin(seed_material), std::end(seed_material));
  82. }
  83. // -----------------------------------------------------------------------------
  84. // y_absl::MakeSeedSeq()
  85. // -----------------------------------------------------------------------------
  86. //
  87. // Constructs an `y_absl::SeedSeq` salting the generated values using
  88. // implementation-defined entropy. The returned sequence can be used to create
  89. // equivalent bit generators correlated using this sequence.
  90. //
  91. // Example:
  92. //
  93. // auto my_seed_seq = y_absl::MakeSeedSeq();
  94. // std::mt19937 rng1(my_seed_seq);
  95. // std::mt19937 rng2(my_seed_seq);
  96. // EXPECT_EQ(rng1(), rng2());
  97. //
  98. SeedSeq MakeSeedSeq();
  99. Y_ABSL_NAMESPACE_END
  100. } // namespace y_absl
  101. #endif // Y_ABSL_RANDOM_SEED_SEQUENCES_H_