11 KiB
[rand.req.eng]
29 Numerics library [numerics]
29.5 Random number generation [rand]
29.5.3 Requirements [rand.req]
29.5.3.4 Random number engine requirements [rand.req.eng]
A random number engine (commonly shortened to engine)e of type E is a uniform random bit generator that additionally meets the requirements (e.g., for seeding and for input/output) specified in this subclause.
At any given time,e has a state ei for some integer i ⥠0.
Upon construction,e has an initial state e0.
An engine's state may be established via a constructor, a seed function, assignment, or a suitable operator>>.
E's specification shall define:
the size of E's state in multiples of the size of result_type, given as an integral constant expression;
the transition algorithmTA by which e's state ei is advanced to its successor stateei+1; and
the generation algorithmGA by which an engine's state is mapped to a value of type result_type.
A class E that meets the requirements of a uniform random bit generator also meets the requirements of a random number engine if the expressions shown in Table 127 are valid and have the indicated semantics, and if E also meets all other requirements of [rand.req.eng].
In Table 127 and throughout this subclause:
T is the type named by E's associated result_type;
e is a value of E, v is an lvalue of E, x and y are (possibly const) values of E;
s is a value of T;
q is an lvalue meeting the requirements of a seed sequence;
z is a value of type unsigned long long;
os is an lvalue of the type of some class template specialization basic_ostream<charT, traits>; and
is is an lvalue of the type of some class template specialization basic_istream<charT, traits>;
where charT and traits are constrained according to [strings] and [input.output].
Table 127 — Random number engine requirements [tab:rand.req.eng]
| ð Expression |
Return type | Pre/post-condition | Complexity |
|---|---|---|---|
| ð E() |
Creates an engine with the same initial state as all other default-constructed engines of type E. | O(size of state) | |
| ð E(x) |
Creates an engine that compares equal to x. | O(size of state) | |
| ð E(s) |
Creates an engine with initial state determined by s. | O(size of state) | |
| ð E(q)240 |
Creates an engine with an initial state that depends on a sequence produced by one call to q.generate. | same as complexity of q.generate called on a sequence whose length is size of state | |
| ð e.seed() |
void | Postconditions: e == E(). | same as E() |
| ð e.seed(s) |
void | Postconditions: e == E(s). | same as E(s) |
| ð e.seed(q) |
void | Postconditions: e == E(q). | same as E(q) |
| ð e() |
T | Advances e's state ei to ei+1 =TA(ei) and returns GA(ei). | per [rand.req.urng] |
| ð e.discard(z)241 |
void | Advances e's state ei to ei+z by any means equivalent to z consecutive calls e(). | no worse than the complexity of z consecutive calls e() |
| ð x == y |
bool | This operator is an equivalence relation. With Sx and Sy as the infinite sequences of values that would be generated by repeated future calls to x() and y(), respectively, returns true if Sx=Sy; else returns false. |
O(size of state) |
| ð x != y |
bool | !(x == y). | O(size of state) |
E shall meet theCpp17CopyConstructible (Table 32) and Cpp17CopyAssignable (Table 34) requirements.
These operations shall each be of complexity no worse than O(size of state).
On hosted implementations, the following expressions are well-formed and have the specified semantics.
os << x
Effects: With os.fmtflags set toios_base::dec|ios_base::left and the fill character set to the space character, writes to os the textual representation of x's current state.
In the output, adjacent numbers are separated by one or more space characters.
Postconditions: The os.fmtflags and fill character are unchanged.
Result: reference to the type of os.
Returns: os.
Complexity: O(size of state)
is >> v
Preconditions: is provides a textual representation that was previously written using an output stream whose imbued locale was the same as that of is, and whose type's template specialization argumentscharT and traits were respectively the same as those of is.
Effects: With is.fmtflags set to ios_base::dec, sets v's state as determined by reading its textual representation from is.
If bad input is encountered, ensures that v's state is unchanged by the operation and calls is.setstate(ios_base::failbit) (which may throw ios_base::failure ([iostate.flags])).
If a textual representation written via os << x was subsequently read via is >> v, then x == v provided that there have been no intervening invocations of x or of v.
Postconditions: The is.fmtflags are unchanged.
Result: reference to the type of is.
Returns: is.
Complexity: O(size of state)
This constructor (as well as the subsequent corresponding seed() function) can be particularly useful to applications requiring a large number of independent random sequences.
This operation is common in user code, and can often be implemented in an engine-specific manner so as to provide significant performance improvements over an equivalent naive loop that makes z consecutive calls e().