Does one need to call srand() C function per thread or per process to seed the randomizer?

后端 未结 4 1565
借酒劲吻你
借酒劲吻你 2021-01-11 16:46

The caption pretty much says it.

PS. This is for C++ Windows program.

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-11 16:54

    According to the MSDN documentation on srand() (assuming you are using Microsoft's C runtime library), the seed is thread-local, so you need to call srand() for each thread that is using rand(). Note that this may not be the case in other implementations.

    Quoting from MSDN:

    The srand function sets the starting point for generating a series of pseudorandom integers in the current thread.

提交回复
热议问题