views:

63

answers:

1

I need help on which random number generator to use simultaneously from multiple threads and get less execution time

+1  A: 

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.

Jerry Coffin