Does C++ constrain the period of std::rand?

Does the C++ standard place any constraints on the minimum / maximum period of the sequence returned by std::rand?

>Solution :


28.5.10 says "The rand and srand functions have the semantics specified in the C standard library."

C17 standard only has this to say about rand:

The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.
The rand function is not required to avoid data races with other calls to pseudo-random sequence generation functions. The implementation shall behave as if no library function calls the rand function.
The rand function returns a pseudo-random integer.
The value of the RAND_MAX macro shall be at least 32767

with a footnote

"There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs."

No constraints on period, quality exist. A function returning 0 and 1 alternatively would be conforming to the standard.

Leave a Reply