英文:
Does C++ constrain the period of std::rand?
问题
C++标准对std::rand
返回的序列的最小/最大周期是否有任何约束?
英文:
Does the C++ standard place any constraints on the minimum / maximum period of the sequence returned by std::rand
?
答案1
得分: 8
以下是已翻译的内容:
28.5.10说:“rand和srand函数具有C标准库中指定的语义。”
C17标准只对rand
有以下说明:
“rand函数计算一个范围在0到RAND_MAX之间的伪随机整数序列。”
“rand函数不需要避免与其他伪随机序列生成函数的数据竞争。实现应该表现得好像没有库函数调用rand函数。”
“rand函数返回一个伪随机整数。”
“RAND_MAX宏的值至少为32767”
带有注释的一段:
“对于生成的随机序列的质量没有保证,一些实现已知生成具有非常非随机的低位比特的序列。具有特定要求的应用程序应使用已知足够满足其需求的生成器。”
没有关于周期和质量的限制。返回交替的0和1的函数符合标准。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论