That's how you seed the random number generator. Because the time is somewhat random, you get a different starting-point for the random number generator every time you run your program.Code:srand(time(NULL));
If your program has a loop, make sure you seed only once outside the loop. If the loop runs fast, it can be re-seeded with the same time, and you'll get the same "random" number more than once.
This is from cppreference.comtime
time_t time( time_t *time );
The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current time is stored in time.
void srand( unsigned seed );
The function srand() is used to seed the random sequence generated by rand(). For any given seed, rand() will generate a specific "random" sequence over and over again.
srand( time(NULL) );
for( i = 0; i < 10; i++ )
printf( "Random number #%d: %d\n", i, rand() );