S
Siam
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( ). This meant that every call to random( ) from my language
ended up returning the same number (41, as it happens). To get round
this, I made each thread's constructor call srand on consecutive seeds,
so that the first thread calls srand(1), the next thread srand(2) and
so on. However, the microsoft compiler (but not g++) has more odd
behaviour when generating random numbers from consecutive seeds. In
short, I noticed my calls to random( ) still didn't produce random
numbers. Essentially, I narrowed it down to the following problem:
int main()
{
for(int index=0; index<20; index++){
srand(index);
cout << rand() << endl;
}
return 0;
}
The code above returns the sequence 41, 45, 48, 51, 54, 58, 61, 64,
68....
For some reason, consecutive seeds return closely related numbers on
the first call to rand(). I've tried popping the first rand( ), then
returning the 2nd rand( ) from each seed, but these are still related
(though not quite as strongly as the first calls).
Three questions:
1) Why has it been chosen that each thread gets independent use of
rand( )
2) Why does the microsoft compiler behave like this? Whats the point in
different seeds if they're all related?
3) Any work-arounds anyone can think of?
Thanks a lot
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( ). This meant that every call to random( ) from my language
ended up returning the same number (41, as it happens). To get round
this, I made each thread's constructor call srand on consecutive seeds,
so that the first thread calls srand(1), the next thread srand(2) and
so on. However, the microsoft compiler (but not g++) has more odd
behaviour when generating random numbers from consecutive seeds. In
short, I noticed my calls to random( ) still didn't produce random
numbers. Essentially, I narrowed it down to the following problem:
int main()
{
for(int index=0; index<20; index++){
srand(index);
cout << rand() << endl;
}
return 0;
}
The code above returns the sequence 41, 45, 48, 51, 54, 58, 61, 64,
68....
For some reason, consecutive seeds return closely related numbers on
the first call to rand(). I've tried popping the first rand( ), then
returning the 2nd rand( ) from each seed, but these are still related
(though not quite as strongly as the first calls).
Three questions:
1) Why has it been chosen that each thread gets independent use of
rand( )
2) Why does the microsoft compiler behave like this? Whats the point in
different seeds if they're all related?
3) Any work-arounds anyone can think of?
Thanks a lot