Why is rand() not so random after fork?

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

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


        
相关标签:
7条回答
  • 2020-12-03 22:45

    This solve the problem:

    srand48((long int)time(NULL));
    i= (lrand48()/rand()+1) % 123
    

    I havent tested with fork, but inside a for calling 100 times it works.

    seed with the pid number. It's a little but difficult to solve problem.

    This was in some page: "this worked srand(time(0)+getpid()); but I had to call this within the case 0 i.e child process".

    0 讨论(0)
提交回复
热议问题