Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does one need to call srand() C function per thread or per process to seed the randomizer?

The caption pretty much says it.

PS. This is for C++ Windows program.

like image 584
ahmd0 Avatar asked Apr 15 '12 05:04

ahmd0


1 Answers

According to the MSDN documentation on srand() (assuming you are using Microsoft's C runtime library), the seed is thread-local, so you need to call srand() for each thread that is using rand(). Note that this may not be the case in other implementations.

Quoting from MSDN:

The srand function sets the starting point for generating a series of pseudorandom integers in the current thread.

like image 109
dreamlax Avatar answered Oct 07 '22 18:10

dreamlax