Try to explain as simply as possible...I've only covered header files: <iostream>, <iomanip>, <cmath>, and just now being introduced to <ctime> and <cstdlib> (I think that's the header file name).
How does srand() use time to generate random numbers?
Collapse
X
-
srand() does NOT use time to generate random numbers.
The argument to srand() is an unsigned int. You can think of this argument as selecting a sequence of random numbers for successive calls to rand() -- a unique sequence for each value of the argument.
Sometimes you want to have the same sequence of random numbers each time your program runs. More often, you prefer a different sequence because that feels more ... random. To get a different sequence your program needs to pass a different value to srand() each time it runs. A convenient way to get a different value is by deriving one from the time-of-day that the program is executed. It is up to you to obtain/compute/derive the value passed to srand(). -
Srand is the random number generator that takes a seed value. Passing srand the time value is a good way to start a unique sequence of random numbers. If you want the random number sequence to be repeatable, then use the same integer value as a seed.
Typically, when creating a string of random numbers, you call srand first with the seed value, and then call rand for all of the rest of the random numbers. Try this link: http://www.cplusplus.com/reference/c.../cstdlib/rand/Comment
Comment