Seeding Random Numbers in Multiple Threads?

H

HumanJHawkins

I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.

Is there a best practice for seeding multiple random number generators
at once?

Thanks in advance!
 
S

Steve Pope

HumanJHawkins said:
I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.

Is there a best practice for seeding multiple random number generators
at once?

Obtain "seed" from the current time, then use

seed
seed + 1000
seed + 2000
seed + 3000

Steve
 
P

peter koch

HumanJHawkins skrev:
I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.
If you are talking about the standard random generator and srand, you
better check the implementation. Most probably it is not thread-safe.
In any case, by protecting rand() with a mutex (of some type - a
windows critical_section looks best), there is no reason to seed more
than once.
Is there a best practice for seeding multiple random number generators
at once?
If you use e.g. boost and let each thread have its own generator you
could e.g. use time and add the threadid to that value.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top