Search code examples
c++csrand

Loss of precision - warning with usage of srand


I am trying to use srand along with time for randomized value. If I look at srand function it takes unsigned int - but if we initialize it with time which seems to have long long value - so compiler keeps complaining about loss of precision warning. Any better way to do things ?

// srand definition
void srand (unsigned int seed);
// using srand with time to seed random value
srand(time(NULL));

warning 712: (Info -- Loss of precision (arg. no. 1) (long long to unsigned int))


Solution

  • If you are using C++, look into <random>. It has much better PRNGs.

    If you're limited to rand, you can, in this case, ignore the warning, or better, tell the compiler you don't care by explicitly casting (unsigned)time(NULL).