Srand (Time (NULL)); we are familiar with C + + random functions, using time to do the seed. However, the random seed is different in the multithreaded environment if you want to randomly make different random numbers in the sub-threads. But time in seconds, slightly insufficient, so refer to this article to solve this problem:
/*Initialize random seed*/voidInitrand () {//if high-performance precision counters are supported, use them to initialize random seeds (microsecond level)Large_integer nfrequency; if(:: QueryPerformanceFrequency (&nfrequency)) {Large_integer nstartcounter; :: QueryPerformanceCounter (&nstartcounter); :: Srand ((unsigned) nstartcounter.lowpart); } Else //Otherwise initialize the random seed (millisecond) using the current system time { structTimeb STB; :: Ftime (&STB); :: Srand ((unsigned) stb.millitm); }}
Concurrent Programming: c++11 random number repetition problem in multi-threading