How often should I call srand() in a C++ application?

后端 未结 4 1938
轮回少年
轮回少年 2020-11-30 14:08

I have a C++ application which calls rand() in various places. Do I need to initialize srand() regularly to ensure that rand() is reasonably random, or is it en

4条回答
  •  时光说笑
    2020-11-30 14:17

    If you have only a single thread, seed once. If you reseed often, you might actually break some of the statistical properties of the random numbers. If you have multiple threads, don't use rand at all, but rather something threadsafe like drand48_r, which lets you maintain a per-thread state (so you can seed once per thread).

提交回复
热议问题