What do you mean by "no getting any results"?
rand() always returns a result.
It doesn't really matter where you call srand() as long as you don't call it more than once. Calling srand() more than once in the same second will reset the RNG to return the same set of numbers.
If this doesn't answer your question, you are going to have to be more specific.
I don't understand why you don't want to put the call to srand() in main.
That is the best way to ensure it only gets called once.
Placing the call to srand() in a class constructor means that srand() will get called every time you construct an instance of that class. Resetting the RNG every time you construct an instance of the class is NOT a good idea.
well,if i don't need to use that one function from that one class i don't need srand at all.so thats why.
i'm trying to create a libary of functions for encrypting and decrypting,and i don't want it to have needs like you must srand before using this class or whatever.
If your encrypt/decrypt class is a singleton pattern, then I have no problem calling srand() from it's constructor.
If you're using rand() to seed your encryption algorithm, not calling srand() at all is not a good idea since the RNG will always return the same sequence of numbers. Pretty easy to crack.
If your encrypt/decrypt class is not a singleton pattern, then I would suggest a static flag in your class to indicate if srand() has been called or not.
hmm,i don't understand the last part about adding a static flag to my class.
i looked up singleton patterns,and have a hard time understanding them aswell.Can you explain further?
A singleton pattern ensures that only one instance of the class can exist.
As far as the static flag:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class MyEncryptDecrypt
{ staticbool srand_called; // Single flag for all instances of class
public:
MyEncryptDecrypt();
...
};
MyEncryptDecrypt::srand_called = false;
MyEncryptDecrypt::MyEncryptDecrypt ()
{ if (! srand_called)
{ srand (time(NULL));
srand_called = true;
}
}