srand(time(0)) and random number generation

srand() gives the random function a new seed, a starting point (usually random numbers are calculated by taking the previous number (or the seed) and then do many operations on that number to generate the next).

time(0) gives the time in seconds since the Unix epoch, which is a pretty good “unpredictable” seed (you’re guaranteed your seed will be the same only once, unless you start your program multiple times within the same second).

Leave a Comment