1 /* boost random/seed_seq.hpp header file
3 * Copyright Steven Watanabe 2010
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: seed_seq.hpp 85813 2013-09-21 20:17:00Z jewillco $
14 #ifndef BOOST_RANDOM_SEED_SEQ_HPP
15 #define BOOST_RANDOM_SEED_SEQ_HPP
17 #include <boost/config.hpp>
18 #include <boost/cstdint.hpp>
19 #include <boost/range/begin.hpp>
20 #include <boost/range/end.hpp>
26 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
27 #include <initializer_list>
34 * The class @c seed_seq stores a sequence of 32-bit words
35 * for seeding a \pseudo_random_number_generator. These
36 * words will be combined to fill the entire state of the
41 typedef boost::uint_least32_t result_type;
43 /** Initializes a seed_seq to hold an empty sequence. */
45 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
46 /** Initializes the sequence from an initializer_list. */
48 seed_seq(const std::initializer_list<T>& il) : v(il.begin(), il.end()) {}
50 /** Initializes the sequence from an iterator range. */
52 seed_seq(Iter first, Iter last) : v(first, last) {}
53 /** Initializes the sequence from Boost.Range range. */
55 explicit seed_seq(const Range& range)
56 : v(boost::begin(range), boost::end(range)) {}
59 * Fills a range with 32-bit values based on the stored sequence.
61 * Requires: Iter must be a Random Access Iterator whose value type
62 * is an unsigned integral type at least 32 bits wide.
65 void generate(Iter first, Iter last) const
67 typedef typename std::iterator_traits<Iter>::value_type value_type;
68 std::fill(first, last, static_cast<value_type>(0x8b8b8b8bu));
69 std::size_t s = v.size();
70 std::size_t n = last - first;
77 std::size_t p = (n - t) / 2;
78 std::size_t q = p + t;
79 std::size_t m = (std::max)(s+1, n);
80 value_type mask = 0xffffffffu;
81 for(std::size_t k = 0; k < m; ++k) {
83 *(first + k%n) ^ *(first + (k+p)%n) ^ *(first + (k+n-1)%n);
85 r1 = (r1 * 1664525u) & mask;
88 (k <= s) ? k % n + v[k - 1] :
90 *(first + (k+p)%n) = (*(first + (k+p)%n) + r1) & mask;
91 *(first + (k+q)%n) = (*(first + (k+q)%n) + r2) & mask;
94 for(std::size_t k = m; k < m + n; ++k) {
96 (*(first + k%n) + *(first + (k+p)%n) + *(first + (k+n-1)%n))
99 r3 = (r3 * 1566083941u) & mask;
100 value_type r4 = r3 - k%m;
101 *(first + (k+p)%n) ^= r3;
102 *(first + (k+q)%n) ^= r4;
106 /** Returns the size of the sequence. */
107 std::size_t size() const { return v.size(); }
108 /** Writes the stored sequence to iter. */
110 void param(Iter out) { std::copy(v.begin(), v.end(), out); }
112 std::vector<result_type> v;