Does stdlib's rand() always give the same sequence?

前端 未结 6 1871
甜味超标
甜味超标 2020-12-07 02:24

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 ran

6条回答
  •  悲&欢浪女
    2020-12-07 02:53

    No.

    The C standard says:

    If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated.

    But nowhere does it say what the sequence of pseudo-random numbers actually is - so it differs across implementations.

    The only guarantee made is that rand() will give the same sequence of numbers for a given seed for a given implementation. There's no guarantee that the sequence will be the same across different machines or different architectures - and it almost certainly won't be.

提交回复
热议问题