srand(), rand() multi-threaded

vc 10.0 source code

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}

           Multithreading to collect random numbers, each thread must srand() once instead of the main thread srand() once, this is because the internal state information of the random number generator is stored in the thread private data; the seed is not initialized with srand in the thread , The threads have the same initial value and generate the same random number sequence.

Guess you like

Origin blog.csdn.net/beebeeyoung/article/details/107802874