# rand() and threads [C++]

This topic is 2557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
Have you looked at boost::random? I have no knowledge of its speed, but I'm sure you can find information on it online.

##### Share on other sites

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().

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 on other sites
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

srand( reinterpret_cast<unsigned int>( &pThreadParams ) );

##### Share on other sites
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 on other sites
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 on other sites
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 on other sites
Give the c++0x generators a go if your compiler supports them. Alternatively boost, which contains more or less the same thing

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631768
• Total Posts
3002240
×