是否STDLIB兰特()总是给相同的序列? [英] Does stdlib's rand() always give the same sequence?
问题描述
我很喜欢能反复生成同一组的伪随机数据,特别是调整实验code。经过观察我要说的是兰特()
似乎给相同的数字序列中的每个时间*。
I quite like being able to generate the same set of pseudo-random data repeatedly, especially with tweaking experimental code. Through observation I would say that rand()
seems to give the same sequence of numbers each time*.
它是保证做到这一点反复执行同一台机器上/不同的机器/不同体系结构?
Is it guaranteed to do this for repeated executions on the same machine / for different machines / for different architectures?
*对于相同的种子明显。
*For the same seed obviously.
推荐答案
是,鉴于该程序的相同的环境。从C标准和教派; 7.20.2.2/2,
Yes, given the same environment for the program. From the C standard §7.20.2.2/2,
的函数srand
函数使用的参数作为伪随机数的新序列的种子由后续调用返回到兰特
。 如果函数srand
然后用相同的种子值调用,伪随机数序列应重复。如果兰特
已经作出,同样的顺序应为当函数srand $ C产生的任何调用
函数srand
之前调用$ C>第一次被调用为1的种子值。
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. Ifrand
is called before any calls tosrand
have been made, the same sequence shall be generated as whensrand
is first called with a seed value of 1.
当然,这假定它使用相同的实现细节(即同一台机器上,在同一时间执行相同的库)。 C标准并不强制标准的随机数生成算法,因此,如果您使用不同的C标准库运行该程序,可以得到不同的随机数序列。
Of course, this assumes it is using the same implementation detail (i.e. same machine, same library at the same execution period). The C standard does not mandate a standard random number generating algorithm, thus, if you run the program with a different C standard library, one may get a different random number sequence.
查看问题<一href=\"http://stackoverflow.com/questions/922358/consistent-pseudo-random-numbers-across-platforms\">Consistent跨平台的伪随机数的如果你需要一个给定的种子便携式和保证随机数序列。
See the question Consistent pseudo-random numbers across platforms if you need a portable and guaranteed random number sequence with a given seed.
这篇关于是否STDLIB兰特()总是给相同的序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!