What‘s the difference between srand(1) and srand(0)

后端 未结 7 2244
广开言路
广开言路 2020-12-30 02:23

I just found out the hard way that srand(1) resets the PRNG of C(++) to the state before any call to srand (as defined in the reference). However,

7条回答
  •  没有蜡笔的小新
    2020-12-30 03:15

    The reason 1 is specified is because some random number generators will get stuck at zero if the seed is set to zero. For example shift-registers and Multiplicative congruential types i.e. r(n+1) = (A * r(n))mod M.

    Many C implementations use Linear Congruential r(n+1) = (A * r(n) + B) mod M, B <> 0 which don't get stuck.

提交回复
热议问题