aborman
asked on
srand() in .lib in DLL
Using the C Libraries:
I have a DLL which calls a function in one of my .lib's.
The .lib genererates a sequence of rand() numbers. It's function looks like (pseudo-code):
if ( ! seeded ) srand( time() )
get many rand();
What happens is: the first call to the library function gives a nice random number. After that every subsequent call (to the library from the DLL) returns the same sequence as the second call, as if it were never seeded at all.
---
I have also tried seeding with each call to the library, as so:
srand( time () )
get many rand();
This works better, but it also will sometimes return the same sequence once or twice in a row.
---
This second result is good enough, but not quite satisfying.
Anyone know why this is happening? or think of a theoretical reason why it should?
I have a DLL which calls a function in one of my .lib's.
The .lib genererates a sequence of rand() numbers. It's function looks like (pseudo-code):
if ( ! seeded ) srand( time() )
get many rand();
What happens is: the first call to the library function gives a nice random number. After that every subsequent call (to the library from the DLL) returns the same sequence as the second call, as if it were never seeded at all.
---
I have also tried seeding with each call to the library, as so:
srand( time () )
get many rand();
This works better, but it also will sometimes return the same sequence once or twice in a row.
---
This second result is good enough, but not quite satisfying.
Anyone know why this is happening? or think of a theoretical reason why it should?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's not clear what exactly you are doing.
What is 'seeded'?
Does DLL is used by several processes?
Do you use LoadLibrary() to load your dll, how many times?
If you call the lib function in a loop
int array_of_rand[][];
for(...) {
LibFunc_Rand(array_of_rand [i])
}
then it's possible that time() returns the same value for different calls.
rand() is very fast, time() returns SECONDS.
Try to use, say,
srand(rand()*GetTickCount( )+GetTickC ount());
What is 'seeded'?
Does DLL is used by several processes?
Do you use LoadLibrary() to load your dll, how many times?
If you call the lib function in a loop
int array_of_rand[][];
for(...) {
LibFunc_Rand(array_of_rand
}
then it's possible that time() returns the same value for different calls.
rand() is very fast, time() returns SECONDS.
Try to use, say,
srand(rand()*GetTickCount(
ASKER
I did try putting the seed in the DLL attach....Oddly, in this case, calls to the .lib function produced sequences as if it were never seeded ( returning the same sequence with each call )
so, I still don't understand why the sequence restarts if we only seed once. I'm still curious about what's going on.
However, now seeing that the library is called twice within a second explains the sometime repetitive sequence that I'm getting.
This gives me a workable solution.
so, I still don't understand why the sequence restarts if we only seed once. I'm still curious about what's going on.
However, now seeing that the library is called twice within a second explains the sometime repetitive sequence that I'm getting.
This gives me a workable solution.
~Aaron