@@ -22,65 +22,28 @@ namespace folly {
2222
2323namespace detail {
2424
25- // Return the state size needed by RNG, expressed as a number of uint32_t
26- // integers. Specialized for all templates specified in the C++11 standard.
27- // For some (mersenne_twister_engine), this is exported as a state_size static
28- // data member; for others, the standard shows formulas.
29-
30- template <class RNG , typename = void >
31- struct StateSize {
32- // A sane default.
33- using type = std::integral_constant<size_t , 512 >;
34- };
35-
36- template <class RNG >
37- struct StateSize <RNG , void_t <decltype (RNG ::state_size)>> {
38- using type = std::integral_constant<size_t , RNG ::state_size>;
39- };
40-
41- template <class UIntType , UIntType a, UIntType c, UIntType m>
42- struct StateSize <std::linear_congruential_engine<UIntType, a, c, m>> {
43- // From the standard [rand.eng.lcong], this is ceil(log2(m) / 32) + 3,
44- // which is the same as ceil(ceil(log2(m) / 32) + 3, and
45- // ceil(log2(m)) <= std::numeric_limits<UIntType>::digits
46- using type = std::integral_constant<
47- size_t ,
48- (std::numeric_limits<UIntType>::digits + 31 ) / 32 + 3 >;
49- };
50-
51- template <class UIntType , size_t w, size_t s, size_t r>
52- struct StateSize <std::subtract_with_carry_engine<UIntType, w, s, r>> {
53- // [rand.eng.sub]: r * ceil(w / 32)
54- using type = std::integral_constant<size_t , r*((w + 31 ) / 32 )>;
55- };
56-
57- template <typename RNG >
58- using StateSizeT = _t<StateSize<RNG >>;
59-
60- template <class RNG >
61- struct SeedData {
62- SeedData () {
63- Random::secureRandom (seedData.data (), seedData.size () * sizeof (uint32_t ));
25+ struct SeedSeqSecureRandom {
26+ using result_type = uint32_t ;
27+ template <typename Word>
28+ void generate (Word* b, Word* e) {
29+ static_assert (is_non_bool_integral_v<Word>);
30+ static_assert (sizeof (Word) >= sizeof (result_type));
31+ Random::secureRandom (b, (e - b) * sizeof (Word));
6432 }
65-
66- static constexpr size_t stateSize = StateSizeT<RNG >::value;
67- std::array<uint32_t , stateSize> seedData;
6833};
6934
7035} // namespace detail
7136
7237template <class RNG , class /* EnableIf */ >
7338void Random::seed (RNG & rng) {
74- detail::SeedData<RNG > sd;
75- std::seed_seq s (std::begin (sd.seedData ), std::end (sd.seedData ));
76- rng.seed (s);
39+ detail::SeedSeqSecureRandom seq;
40+ rng.seed (seq);
7741}
7842
7943template <class RNG , class /* EnableIf */ >
8044auto Random::create () -> RNG {
81- detail::SeedData<RNG > sd;
82- std::seed_seq s (std::begin (sd.seedData ), std::end (sd.seedData ));
83- return RNG (s);
45+ detail::SeedSeqSecureRandom seq;
46+ return RNG (seq);
8447}
8548
8649} // namespace folly
0 commit comments