Sign in to follow this  
maya18222

rand() and threads [C++]

Recommended Posts

maya18222    194
Using rand() gives me the same sequence of random numbers for each thread, so I looked up rand_s() which doesnt store a global state, however, my tests show that rand_s() is about 25 times slower, which is a huge slow down in my particular case as Im using it on pixel operations. Other than trying to implement my own RNG, is there another solution?

I need it to be as fast or faster than rand().

Share this post


Link to post
Share on other sites
Ashaman73    13715
[quote name='maya18222' timestamp='1311076137' post='4837326']
Using rand() gives me the same sequence of random numbers for each thread, so I looked up rand_s() which doesnt store a global state, however, my tests show that rand_s() is about 25 times slower, which is a huge slow down in my particular case as Im using it on pixel operations. Other than trying to implement my own RNG, is there another solution?

I need it to be as fast or faster than rand().
[/quote]
The problem is, that rand is not defined as threadsafe per default, it depends on the library and how threadsafity is implemented, when at all. When using windows multithreaded CRT rand_s uses a operation function (therefor slower),whereas rand work in a "per thread" environment(thead local space). Best to generate different seeds for each thread and stick to the rand function.

Edit: Ninjaed all the time... too slow while looking up translations etc. <_< Edited by Ashaman73

Share this post


Link to post
Share on other sites
maya18222    194
I thought that rand() used a global state and that rand_s() used TLS, which is why I didnt think reseeding would help. However, I reseeded and it solved the problem. Thanks.

I used the following for anyone else with the same problem

[code]srand( reinterpret_cast<unsigned int>( &pThreadParams ) )[/code];

Share this post


Link to post
Share on other sites
alvaro    21247
I would go with a PRNG class of which each thread has an instance. Internally it can do whatever you are doing now, but if you want to move to your own PRNG in the future, you have a place to put it with a clean interface.

Share this post


Link to post
Share on other sites
rip-off    10976
I'm not sure I like your idea of using the address of an object as the random seed. I've a feeling that statistically these pointers might be tightly grouped in a particular address space. If you want to avoid seeding all your threads with the same time() value, perhaps a simple checksum on the pointer's address compressed to the low N bits XOR the time(). This way, successive runs of your program will certainly have different seeds, and your threads will also have different seeds.

Share this post


Link to post
Share on other sites
alvaro    21247
Any decent PRNG will generate completely different sequences if started from slightly different seeds, so I don't have rip-off's concern. However, I would prefer to have a more reproducible set of pseudo-random numbers. If you can assign your threads numeric ids, I would prefer to use that to seed the PRNGs. If you use time(), make sure to write out its value so you can reproduce everything, in case you need to debug a problem.

Share this post


Link to post
Share on other sites
taz0010    277
Give the c++0x generators a go if your compiler supports them. Alternatively boost, which contains more or less the same thing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this