[rand.util] # 29 Numerics library [[numerics]](./#numerics) ## 29.5 Random number generation [[rand]](rand#util) ### 29.5.8 Utilities [rand.util] #### [29.5.8.1](#seedseq) Class seed_seq [[rand.util.seedseq]](rand.util.seedseq) [🔗](#lib:seed_seq) namespace std {class seed_seq {public:// typesusing result_type = uint_least32_t; // constructors seed_seq() noexcept; template seed_seq(initializer_list il); template seed_seq(InputIterator begin, InputIterator end); // generating functionstemplatevoid generate(RandomAccessIterator begin, RandomAccessIterator end); // property functions size_t size() const noexcept; templatevoid param(OutputIterator dest) const; // no copy functions seed_seq(const seed_seq&) = delete; void operator=(const seed_seq&) = delete; private: vector v; // *exposition only*};} [🔗](#lib:seed_seq,constructor) `seed_seq() noexcept; ` [1](#seedseq-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4198) *Postconditions*: v.empty() is true[.](#seedseq-1.sentence-1) [🔗](#lib:seed_seq,constructor_) `template seed_seq(initializer_list il); ` [2](#seedseq-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4211) *Constraints*: T is an integer type[.](#seedseq-2.sentence-1) [3](#seedseq-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4215) *Effects*: Same as seed_seq(il.begin(), il.end())[.](#seedseq-3.sentence-1) [🔗](#lib:seed_seq,constructor__) `template seed_seq(InputIterator begin, InputIterator end); ` [4](#seedseq-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4228) *Mandates*: iterator_traits​::​value_type is an integer type[.](#seedseq-4.sentence-1) [5](#seedseq-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4233) *Preconditions*: InputIterator meets the [*Cpp17InputIterator*](input.iterators#:Cpp17InputIterator "24.3.5.3 Input iterators [input.iterators]") requirements ([[input.iterators]](input.iterators "24.3.5.3 Input iterators"))[.](#seedseq-5.sentence-1) [6](#seedseq-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4238) *Effects*: Initializes v by the following algorithm:for (InputIterator s = begin; s != end; ++s) v.push_back((*s)mod232); [🔗](#lib:generate,seed_seq) `template void generate(RandomAccessIterator begin, RandomAccessIterator end); ` [7](#seedseq-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4255) *Mandates*: iterator_traits​::​​value_type is an unsigned integer type capable of accommodating 32-bit quantities[.](#seedseq-7.sentence-1) [8](#seedseq-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4260) *Preconditions*: RandomAccessIterator meets the [*Cpp17RandomAccessIterator*](random.access.iterators#:Cpp17RandomAccessIterator "24.3.5.7 Random access iterators [random.access.iterators]") requirements ([[random.access.iterators]](random.access.iterators "24.3.5.7 Random access iterators")) and the requirements of a mutable iterator[.](#seedseq-8.sentence-1) [9](#seedseq-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4266) *Effects*: Does nothing if begin == end[.](#seedseq-9.sentence-1) Otherwise, with s=v.size() and n=end−begin, fills the supplied range [begin,end) according to the following algorithm in which each operation is to be carried out modulo 232, each indexing operator applied to begin is to be taken modulo n, and T(x) is defined as x xor(x rshift27): - [(9.1)](#seedseq-9.1) By way of initialization, set each element of the range to the value 0x8b8b8b8b[.](#seedseq-9.1.sentence-1) Additionally, for use in subsequent steps, let p=(n−t)/2 and let q=p+t, where t=(n≥623) ? 11 : (n≥68) ? 7 : (n≥39) ? 5 : (n≥7) ? 3 : (n−1)/2; - [(9.2)](#seedseq-9.2) With m as the larger of s+1 and n, transform the elements of the range: iteratively for k=0,…,m−1, calculate values r1=1664525â‹T(begin[k]xorbegin[k+p]xorbegin[k−1])r2=r1+⎧⎪⎨⎪⎩s, k=0kmodn+v[k−1], 0 void param(OutputIterator dest) const; ` [13](#seedseq-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4369) *Mandates*: Values of type result_type are writable ([[iterator.requirements.general]](iterator.requirements.general "24.3.1 General")) to dest[.](#seedseq-13.sentence-1) [14](#seedseq-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4373) *Preconditions*: OutputIterator meets the [*Cpp17OutputIterator*](output.iterators#:Cpp17OutputIterator "24.3.5.4 Output iterators [output.iterators]") requirements ([[output.iterators]](output.iterators "24.3.5.4 Output iterators"))[.](#seedseq-14.sentence-1) [15](#seedseq-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4378) *Effects*: Copies the sequence of prepared 32-bit units to the given destination, as if by executing the following statement:copy(v.begin(), v.end(), dest); [16](#seedseq-16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4387) *Throws*: What and when OutputIterator operations of dest throw[.](#seedseq-16.sentence-1) #### [29.5.8.2](#canonical) Function template generate_canonical [[rand.util.canonical]](rand.util.canonical) [🔗](#lib:generate_canonical) `template RealType generate_canonical(URBG& g); ` [1](#canonical-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4404) Let - [(1.1)](#canonical-1.1) r be numeric_limits​::​radix, - [(1.2)](#canonical-1.2) R be g.max()−g.min()+1, - [(1.3)](#canonical-1.3) d be the smaller of digits and numeric_limits​::​digits,[245](#footnote-245 "d is introduced to avoid any attempt to produce more bits of randomness than can be held in RealType.") - [(1.4)](#canonical-1.4) k be the smallest integer such that Rk≥rd, and - [(1.5)](#canonical-1.5) x be ⌊Rk/rd⌋[.](#canonical-1.sentence-1) An [*attempt*](#def:attempt "29.5.8.2 Function template generate_­canonical [rand.util.canonical]") is k invocations of g() to obtain values g0,…,gk−1, respectively, and the calculation of a quantity S given by Formula [29.1](#eq:rand.gencanonical): S=k−1∑i=0(gi−g.min())â‹Ri(29.1) [2](#canonical-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L4426) *Effects*: Attempts are made until S