How does srand() use time to generate random numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AxeMnaS
    New Member
    • Dec 2009
    • 2

    How does srand() use time to generate random numbers?

    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).
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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().

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      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

      • AxeMnaS
        New Member
        • Dec 2009
        • 2

        #4
        Alright, i'll check the link out...and thanks for the explanations...

        Comment

        Working...