|
||||
File indexing completed on 2025-01-30 09:31:49
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: seed_sequences.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // This header contains utilities for creating and working with seed sequences 0020 // conforming to [rand.req.seedseq]. In general, direct construction of seed 0021 // sequences is discouraged, but use-cases for construction of identical bit 0022 // generators (using the same seed sequence) may be helpful (e.g. replaying a 0023 // simulation whose state is derived from variates of a bit generator). 0024 0025 #ifndef ABSL_RANDOM_SEED_SEQUENCES_H_ 0026 #define ABSL_RANDOM_SEED_SEQUENCES_H_ 0027 0028 #include <iterator> 0029 #include <random> 0030 0031 #include "absl/base/config.h" 0032 #include "absl/base/nullability.h" 0033 #include "absl/random/internal/salted_seed_seq.h" 0034 #include "absl/random/internal/seed_material.h" 0035 #include "absl/random/seed_gen_exception.h" 0036 #include "absl/strings/string_view.h" 0037 #include "absl/types/span.h" 0038 0039 namespace absl { 0040 ABSL_NAMESPACE_BEGIN 0041 0042 // ----------------------------------------------------------------------------- 0043 // absl::SeedSeq 0044 // ----------------------------------------------------------------------------- 0045 // 0046 // `absl::SeedSeq` constructs a seed sequence according to [rand.req.seedseq] 0047 // for use within bit generators. `absl::SeedSeq`, unlike `std::seed_seq` 0048 // additionally salts the generated seeds with extra implementation-defined 0049 // entropy. For that reason, you can use `absl::SeedSeq` in combination with 0050 // standard library bit generators (e.g. `std::mt19937`) to introduce 0051 // non-determinism in your seeds. 0052 // 0053 // Example: 0054 // 0055 // absl::SeedSeq my_seed_seq({a, b, c}); 0056 // std::mt19937 my_bitgen(my_seed_seq); 0057 // 0058 using SeedSeq = random_internal::SaltedSeedSeq<std::seed_seq>; 0059 0060 // ----------------------------------------------------------------------------- 0061 // absl::CreateSeedSeqFrom(bitgen*) 0062 // ----------------------------------------------------------------------------- 0063 // 0064 // Constructs a seed sequence conforming to [rand.req.seedseq] using variates 0065 // produced by a provided bit generator. 0066 // 0067 // You should generally avoid direct construction of seed sequences, but 0068 // use-cases for reuse of a seed sequence to construct identical bit generators 0069 // may be helpful (eg. replaying a simulation whose state is derived from bit 0070 // generator values). 0071 // 0072 // If bitgen == nullptr, then behavior is undefined. 0073 // 0074 // Example: 0075 // 0076 // absl::BitGen my_bitgen; 0077 // auto seed_seq = absl::CreateSeedSeqFrom(&my_bitgen); 0078 // absl::BitGen new_engine(seed_seq); // derived from my_bitgen, but not 0079 // // correlated. 0080 // 0081 template <typename URBG> 0082 SeedSeq CreateSeedSeqFrom(URBG* urbg) { 0083 SeedSeq::result_type 0084 seed_material[random_internal::kEntropyBlocksNeeded]; 0085 0086 if (!random_internal::ReadSeedMaterialFromURBG( 0087 urbg, absl::MakeSpan(seed_material))) { 0088 random_internal::ThrowSeedGenException(); 0089 } 0090 return SeedSeq(std::begin(seed_material), std::end(seed_material)); 0091 } 0092 0093 // ----------------------------------------------------------------------------- 0094 // absl::MakeSeedSeq() 0095 // ----------------------------------------------------------------------------- 0096 // 0097 // Constructs an `absl::SeedSeq` salting the generated values using 0098 // implementation-defined entropy. The returned sequence can be used to create 0099 // equivalent bit generators correlated using this sequence. 0100 // 0101 // Example: 0102 // 0103 // auto my_seed_seq = absl::MakeSeedSeq(); 0104 // std::mt19937 rng1(my_seed_seq); 0105 // std::mt19937 rng2(my_seed_seq); 0106 // EXPECT_EQ(rng1(), rng2()); 0107 // 0108 SeedSeq MakeSeedSeq(); 0109 0110 ABSL_NAMESPACE_END 0111 } // namespace absl 0112 0113 #endif // ABSL_RANDOM_SEED_SEQUENCES_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |