Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

srand() in .lib in DLL

Posted on 2000-03-09
4
608 Views
Last Modified: 2013-12-03
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?
0
Comment
Question by:aborman
4 Comments
 
LVL 3

Expert Comment

by:BudVVeezer
ID: 2602931
are you reseeding the rand generator EVERY time you enter the dll file?  I would think that if you put it in each time a process or a thread contacts the dll(in DllMain) you would get much better results...  What may be happening is that the Dll gets releases once it is done being used(MAYBE, but not certain) and so it loses the seed info..  ::shrugs::  Try throwing the srand in THREAD_ATTACH in the DllMain.

~Aaron
0
 
LVL 4

Accepted Solution

by:
wylliker earned 200 total points
ID: 2603294
srand() will generate the same sequence if given the same value as it is a pseudo-random number generator.

If you should call your function twice within the same second - since you are using time() as the seed, you will get the same result.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2603304
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()+GetTickCount());


0
 

Author Comment

by:aborman
ID: 2604987
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question