473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Peculiar rand() case and threads

ds
Hi all,

rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing changed. Alas, rand() is not thread safe
so even though both versions seed with the same number, consecutive
rand() calls do not yield the same number for each thread... I would
like to setup a workaround in the new code only rather than make
changes to the old code as well. Any ideas?

Thanks a lot in advance!!!!

BR

-- ds

Mar 2 '07 #1
5 2297
On Mar 2, 8:56 am, "ds" <junkmailav...@yahoo.comwrote:
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing changed. Alas, rand() is not thread safe
so even though both versions seed with the same number, consecutive
rand() calls do not yield the same number for each thread... I would
like to setup a workaround in the new code only rather than make
changes to the old code as well. Any ideas?
Threads are not part of standard C++, so your question is off-topic
here. See <http://www.parashift.com/c++-faq-lite/how-to-
post.html#faq-5.9>.

<OT>
Just protect calls that affect the random number generation with a
mutex (or whatever the equivalent is called on your system). See this
article on Boost.Threads for a similar example in which the standard
input/output must be protected: <http://www.ddj.com/dept/cpp/
184401518>.
</OT>

Cheers! --M

Mar 2 '07 #2
ds
Hi,

thanks for the reply. My question is more related to rand() rather
than threads ;-) and whether there is some access function. Apparently
there is rand_r() which is reentrant, but it does not yield the same
sequence (or I do not know how to make it do so...). Thanks a lot!!!
BR

-- ds

Mar 2 '07 #3
On Mar 2, 9:58 am, "ds" <junkmailav...@yahoo.comwrote:
thanks for the reply. My question is more related to rand() rather
than threads ;-) and whether there is some access function. Apparently
there is rand_r() which is reentrant, but it does not yield the same
sequence (or I do not know how to make it do so...). Thanks a lot!!!
It's still intimately tied to threading -- if you reduce it to a
single-threaded case, there's no problem. I'd suggest you ask on
comp.programming.threads or a newsgroup for your OS or threading
library.

<OT>
Re-reading your original post, I take it that you want two threads to
use the random number generator simultaneously but without getting
different results. So if the random sequence is { 2, 5, 3, 1 }, you
want both threads to get this sequence in the exact same order (rather
than thread 1 seeing, say, { 2, 3 } and thread 2 seeing { 5, 1 } or
something). If so, I think you'll need to implement some sort of
queueing of the random numbers based on thread id or whatever. Ask for
more details on a more appropriate group, and you might get a more
insightful response.
</OT>

Cheers! --M

Mar 2 '07 #4
On Mar 2, 8:56 am, "ds" <junkmailav...@yahoo.comwrote:
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing changed. Alas, rand() is not thread safe
so even though both versions seed with the same number, consecutive
rand() calls do not yield the same number for each thread... I would
like to setup a workaround in the new code only rather than make
changes to the old code as well. Any ideas?
One not-too-hard suggestion: Ditch the built in rand
and get your own. There are a variety of them available
for free on the net, plus just about every numerical
text will have source code for at least one. It will
not be too hard to find one with better stats than the
built in rand.

Then make yourself a tiny little class that accepts
a seed, then makes calls to your own rand fucntion.
Apart from the rand function, should only be ten or
twenty lines of code.

Then have each thread make an instance of its own
rand generating object. Thread safety should be fairly
easy to have as a built-in.

The built in rand is often not very good. Unless you
are doing nothing more important than writing a game,
and maybe not even then, the built in rand is not
going to provide adequate quality random numbers.
Socks

Mar 2 '07 #5
On 2 Mar 2007 05:56:42 -0800, "ds" <ju***********@yahoo.comwrote:
>Hi all,

rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing changed. Alas, rand() is not thread safe
so even though both versions seed with the same number, consecutive
rand() calls do not yield the same number for each thread... I would
like to setup a workaround in the new code only rather than make
changes to the old code as well. Any ideas?

Thanks a lot in advance!!!!

BR

-- ds
I assume that you want to have both threads use the same series of
random numbers, rather than each use a different half of the series.
That means you will have to remember the numbers rand() generates so
that each number can be passed to both threads in the correct order.

To make the built-in rand() thread safe you could put it into a class
of its own. Off the top of my head, you could use the real rand() to
populate two queues. One queue feeds your "old" thread via one
interface (say rand1())and the other queue feeds your "new" thread via
a second interface (rand2()). When either queue is exhausted, cycle
rand() a few times and add the numbers generated to both queues. Each
thread can only use its appropriate interface, rand1() or rand2().
You say that you don't want to change your old code. Would
#define rand rand1
count?

rossum

Mar 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: eyal.susser | last post by:
I hear rand() is not thread safe. I was using it, foolish man that I am. But what is meant exactly by unsafe? What can happen? Bizzare results from rand()? Something worse? Thanks, Eyal.
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
5
by: vib | last post by:
Hi there, i) Is there any other way to replace the rand()of standard C lib? ii) How efficient is the rand()? Thanks in advance. vib
11
by: Fernando Barsoba | last post by:
Hi all, I think this has been posted many times.. but I tried several recipes for obtaining a random number between , but I can't make them work. So, does anyone know how to generate random...
6
by: Roka | last post by:
Hi all. I'm reading a program which used the sentence below: #define NUM_THREADS 10 ... ... int rand_num; rand_num = 1+ (int) (9.0 * rand() / (RAND_MAX + 1.0)); sleep(rand_num); ... ......
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
8
by: DumRat | last post by:
Hi, I made this simple program. UINT Func(void * x) { cout << rand() << endl; return 0; }
8
by: remlostime | last post by:
i use g++ to generater rand number, now i find that the RAND_MAX is 32367 in my computer, how can i make a bigger rand number( the number is wihin in the integer(2^32-1))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.