Why is rand() not so random after fork?

前端 未结 7 1188
星月不相逢
星月不相逢 2020-12-03 22:05
#include 
#include 
#include 
#include 

int main() {
    int i =10;
    /* initialize random seed:  */
         


        
7条回答
  •  孤街浪徒
    2020-12-03 22:28

    You're not reseeding when you make a child process. The state of the random number generator is exactly the same.

    Even if you seed again in your child, you're seeding with the time with a +/- 1 second granularity. When you fork, it all happens in less than a second.

    Try seeding it with something different and more random.

提交回复
热议问题