Is it good idea to pass uninitialized variable to srand
instead of result of time(NULL)
?
It is one #include
and one function call less.
Example code:
#include <stdlib.h>
int main(void) {
{
usigned seed; //uninitialized
srand(seed);
}
//other code
return 0;
}
instead of
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));
//other code
return 0;
}
No, it isn't.
Reading an uninitialized value results in undefined behavior. It can be zero, it can be semi-random — but as such, it can repeatedly be the same value. It may also cause compilation or runtime errors, or do any other completely unpredictable thing.
Then, someone else compiling your program will notice the compiler warning about uninitialized value and try to fix it. He may fix it correctly, or given complex enough program he may just initialize it to zero. That's how 'small' bugs turn into huge bugs.
Just try your snippet with
srand()
replaced byprintf()
:On my system it repeatedly gives
0
. This means that there's at least one system where your code is broken :).It's not a good idea. Undefined behaviour doesn't guarantee that you won't get the same seed on every run, which would be bad for randomness. It doesn't guarantee that your processor won't halt and catch fire either.
No, accessing uninitialized variable is an undefined behavior. You should prefer
time(NULL)
. Uninitialized variable method may introduce difficult to find bugs or may blow the computer.Most of the time, effect observed would be, (on most implementation) above code would take (read) some leftover value from the stack that may be zero or something else but may be same on multiple runs defeating your purpose.
time
on the other hand is promising.Another point is that uninitialized variables can result in a vulnerability. So it is not only bad design and undefined behaviour, it also can make your program exploitable.
Consider this code:
Possible output:
The next example is a bit more realistic:
The uninitialized variable can be modified by a user.
Input:
Output:
In the first case there are two possibilities:
time(NULL) returns the time of the system which is 99.99% different every time you run the code.
Nothing is perfect random, but using time(NULL) gives you a "more random" number then if you would use the first approach.
You should check function's usage http://www.cplusplus.com/reference/cstdlib/srand/