Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

No.

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading