Files
cppdraft_translate/cppdraft/rand/util.md
2025-10-25 03:02:53 +03:00

10 KiB
Raw Blame History

[rand.util]

29 Numerics library [numerics]

29.5 Random number generation [rand]

29.5.8 Utilities [rand.util]

29.5.8.1 Class seed_seq [rand.util.seedseq]

🔗

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<result_type> v; // exposition only};}

🔗

seed_seq() noexcept;

1

#

Postconditions: v.empty() is true.

🔗

template<class T> seed_seq(initializer_list<T> il);

2

#

Constraints: T is an integer type.

3

#

Effects: Same as seed_seq(il.begin(), il.end()).

🔗

template<class InputIterator> seed_seq(InputIterator begin, InputIterator end);

4

#

Mandates: iterator_traits::value_type is an integer type.

5

#

Preconditions: InputIterator meets the Cpp17InputIterator requirements ([input.iterators]).

6

#

Effects: Initializes v by the following algorithm:for (InputIterator s = begin; s != end; ++s) v.push_back((*s)mod232);

🔗

template<class RandomAccessIterator> void generate(RandomAccessIterator begin, RandomAccessIterator end);

7

#

Mandates: iterator_traits::value_type is an unsigned integer type capable of accommodating 32-bit quantities.

8

#

Preconditions: RandomAccessIterator meets the Cpp17RandomAccessIterator requirements ([random.access.iterators]) and the requirements of a mutable iterator.

9

#

Effects: Does nothing if begin == end.

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)

    By way of initialization, set each element of the range to the value 0x8b8b8b8b. 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)

    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<k≤skmodn, s<k and, in order, increment begin[k+p] by r1, increment begin[k+q] by r2, and set begin[k] to r2.

  • (9.3)

    Transform the elements of the range again, beginning where the previous step ended: iteratively for k=m,…,m+n−1, calculate values
    r3=1566083941â‹T(begin[k]+begin[k+p]+begin[k−1])r4=r3−(kmodn) and, in order, update begin[k+p] by xoring it with r3, update begin[k+q] by xoring it with r4, and set begin[k] to r4.

10

#

Throws: What and when RandomAccessIterator operations of begin and end throw.

🔗

size_t size() const noexcept;

11

#

Returns: The number of 32-bit units that would be returned by a call to param().

12

#

Complexity: Constant time.

🔗

template<class OutputIterator> void param(OutputIterator dest) const;

13

#

Mandates: Values of type result_type are writable ([iterator.requirements.general]) to dest.

14

#

Preconditions: OutputIterator meets the Cpp17OutputIterator requirements ([output.iterators]).

15

#

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

#

Throws: What and when OutputIterator operations of dest throw.

29.5.8.2 Function template generate_canonical [rand.util.canonical]

🔗

template<class RealType, size_t digits, class URBG> RealType generate_canonical(URBG& g);

1

#

Let

r be numeric_limits::radix,

R be g.max()−g.min()+1,

d be the smaller of digits and numeric_limits::digits,245

k be the smallest integer such that Rk≥rd, and

x be ⌊Rk/rd⌋.

An attempt is k invocations of g() to obtain values g0,…,gk−1, respectively, and the calculation of a quantity S given by Formula 29.1:

S=k−ˆ‘i=0(gi−g.min())â‹Ri(29.1)

2

#

Effects: Attempts are made until S<xrd.

[Note 1:

When R is a power of r, precisely one attempt is made.

— end note]

3

#

Returns: ⌊S/x⌋/rd.

[Note 2:

The return value c satisfies0≤c<1.

— end note]

4

#

Throws: What and when g throws.

5

#

Complexity: Exactly k invocations of g per attempt.

6

#

[Note 3:

If the values gi produced by g are uniformly distributed, the instantiation's results are distributed as uniformly as possible.

Obtaining a value in this way can be a useful step in the process of transforming a value generated by a uniform random bit generator into a value that can be delivered by a random number distribution.

— end note]

7

#

[Note 4:

When R is a power of r, an implementation can avoid using an arithmetic type that is wider than the output when computing S.

— end note]

245)245)

d is introduced to avoid any attempt to produce more bits of randomness than can be held in RealType.