gpt4 book ai didi

c++ - C++生成一定范围内均匀分布的随机数

转载 作者:行者123 更新时间:2023-11-30 05:37:58 25 4
gpt4 key购买 nike

我知道有类似的问题。
但是我的有不同的设置。

  • 必须使用rand()
  • 必须使用 srand() 从用户输入中获取种子
  • 只使用标准库

我不知道是否可以得到均匀分布的随机数,是否必须使用 random 库?

最佳答案

当涉及到像 rand() 这样的继承函数时,C++ 标准主要遵从 C 标准。和 srand() .

不幸的是,C 标准中没有关于rand() 的内容提供均匀分布。事实上,它在脚注中指出(我强调):

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.

如果您想要随机数的特定属性,您真的应该使用 <random> 中的 C++ 内容(是的,我知道您似乎对此打了折扣,但您可能必须做出权衡:要么使用它们,要么不保证特定的分布)。

关于c++ - C++生成一定范围内均匀分布的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com