[英]c++ random number and wait parent to all children when process
这是我的代码
#include <iostream>
#include <string>
#include <ctime>
int main()
{
for (int i=0;i<3;i++) {
if((pid()==fork())==0){
break;
}
}
if(pid==0) {
// son process
srand(time(NULL));
int r = rand()%4+1;
cout<<"pid:"<<getpid() <<"num": "<<r<<endl;
}
else{
//parent process
}
}
我在所有过程中都遇到了问题随机数,如何重置rand()? 我已经尝试过对每个无效的进程使用不同的srand()。
time(NULL)+getpid()
放到所有兰特的不同种子上;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.