random numbers and multiple srand calls

前端 未结 3 1877
长情又很酷
长情又很酷 2021-01-18 09:24

I\'m writing a program that will generate numerous random numbers in loops. I\'m trying to make the numbers somewhat less predictable (not only for security, but to avoid c

3条回答
  •  广开言路
    2021-01-18 10:08

    let's break this question into two separate ones.

    1. if you worry about race conditions when accessing random number generator, create a mutex or other synchronization primitive to avoid this.

    2. if you think about calling srand() several times, please, don't. the reason behind this is the random generator initialization routine which sets up the variables based on seed has worse random characteristics, gives worse performance than the random generator itself and should be not used as a substitute.

提交回复
热议问题