Simplicity Simplicity - 1 year ago 141
C++ Question

srand(time(0)) and random number generation


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?


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download