Simplicity - 1 year ago 99

C++ Question

`srand(time(0))`

is used in C++ to help in the generation of random numbers by seeding rand with a starting value.

But, can you explain what it exactly does?

Thanks.

Answer Source

`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).