help with seeding random in function

Im not sure why the seed isn't working... xcode is saying "Implicit conversion loses integer precision: 'time_t'(aka 'long') to 'unsigned int' ".
Thanks for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  void MultiStatsArray::randomFill()
{
	//seed the random number generator
    srand(time(0));

    for ( int r =0; r < ROWSIZE; r++)
    {
        for ( int c=0; c < COLSIZE; c++)
        {
            data[r][c]= rand() %101;
        }
    }
	
}
This is because time() returns long and srand() takes unsigned int. Although both are numeric datatypes, long is larger than int, and if the value returned by time() is big enough, then due to lost precision some other value will be passed to srand(). Nothing really bad happens due to this in srand() application here, and that's why you should get a warning rather than an error - except if you run compiler with -wall (or 'fail at warning') flag.

To fix that explicitly cast time(0) to unsigned int like this:
srand((unsigned int)time(0));

Note that it would fix the warning, because there is no reason for srand() not to work, that is not to randomize the output between runs of this code.
Last edited on
Topic archived. No new replies allowed.