我的目标是生成 2D 或 3D 几何图形而不必将其存储在磁盘上,因此我的目标是拥有任何类型的功能,而不是根据小种子生成相同的值。我并不是要寻找随机值,但如果在给定相同种子时返回相同的“随机”垃圾数据,那就是我正在寻找的东西。
如果我给 srand()
相同的整数,我会从 rand() 中得到相同的序列。这是预期的功能吗?如果不是,是否有已知的标准函数设计用于执行相同的操作?
虽然我在 ideone 和我的电脑上都试过这个并且得到了不同的结果,但我可以理解这些函数的实现没有被描述,所以这就解释了。
最佳答案
If I give srand() the same integer, I get the same sequence out of rand(). Is that an intended feature?
是的,参见 7.20.2.2:
7.20.2.2 The srand function
[...] Description
The
srand
function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls torand
. Ifsrand
is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated.
但是,这仅适用于 srand
/rand
的相同实现。另一个实现可能不使用相同的算法,因此不会产生相同的序列。
If not, are there known standard functions designed to do the same thing ?
好吧,函数是标准的,但只是在它们的行为中,而不是实际值(参见上面的实现注释)。您最好使用来自 C++11 predefined random number generators 的特定生成器,因为它们是标准化的。
关于c++ - rand() 可以用来生成可预测的数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23690226/