I need help on which random number generator to use simultaneously from multiple threads and get less execution time
I need help on which random number generator to use simultaneously from multiple threads
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Virtually any random number generator has some saved state. To get correct behavior when called from multiple threads, it’ll have to either use thread local storage for its stored state, or else use synchronized access to the stored state.
If it uses synchronized access to shared state, that’s usually going to be fairly slow.
If it uses thread local storage, that introduces the extra problem that each thread that uses it has to seed the generator separately, and ensure that the seeds aren’t the same (e.g. if you use the common
srand(time(NULL))in each thread, chances are pretty good that multiple threads will get the same seed and produce the same sequence of outputs. Once you’ve seeded it reasonably, however, invocations will generally be reasonably fast — the only slowdown compared to single-threaded is (possibly) having a level of indirection to get to the thread local storage.