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 5 2324
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
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
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.programmin g.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
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
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 reproducibilit y 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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 500,000 consecutive rand
calls then is the rand() algorithm going to guarantee me that no
floating window of calls over that 500,000 rand calls will have the
same value twice? The only way that would work is if the series was
identical everytime.
|
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
|
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 number x, for 0<= x <=1??
Thanks,
FBM
|
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);
... ...
(The program is long so I used a part of it.)
| |
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 my language simply
propogates up to the rand( ) function from c++. For some reason, C++
will give each thread independent use of their own rand( ) function, so
that a rand( ) call from one thread won't affect another thread's call
to rand( )....
|
by: DumRat |
last post by:
Hi,
I made this simple program.
UINT Func(void * x)
{
cout << rand() << endl;
return 0;
}
|
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))
|
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 of two (or near a power of
two), that this is not an issue. The upper bits will participate
equally in the result with the lower. Am I missing something?
Thanks!
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |