>this is considered bad practice by most...
Yes, but not for the reason that you intended. Let's say for the sake of argument that we follow the convention of not using NULL in favor of 0:
Code:
#include <cstdlib>
#include <ctime>
...
std::srand ( std::time ( 0 ) );
This is all well and good, right? Well, no, not really. time_t is a funky little creature that is highly restricted by the C++ standard. Basically all you can be sure of is that it is an arithmetic type that can be compared to -1 cast to time_t. For the most part you can expect this to work:
Code:
std::srand ( static_cast<unsigned int> ( std::time ( 0 ) ) );
But it still isn't portable because you have no idea what the type or representation of time_t is. A good solution I've seen suggested is to take a hash of the bits rather than using the value directly:
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>
unsigned int get_seed()
{
time_t now = std::time ( 0 );
unsigned char *p = reinterpret_cast<unsigned char *> ( &now );
unsigned int seed = 0;
for ( size_t i = 0; i < sizeof now; i++ )
seed = seed * ( std::numeric_limits<unsigned char>::max() + 2U ) + p[i];
return seed;
}