C++是否限制了std::rand的周期?

huangapple go评论59阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月19日 23:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定