time

Discussion in 'C Programming' started by Bill Cunningham, Nov 20, 2012.

  1. I am using the time and date in my code using asctime () time(), and
    localtime(). I know in POSIX they are obsolete but I am concerned with
    c89-99. strftime() is suppoed to be the function used now. I get the time
    and date just fine with the code I've written but I want to use the time to
    seed a random number generator. I don't know if I want seconds from epoch or
    what. Any input?

    Bill
     
    Bill Cunningham, Nov 20, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    Geoff Guest

    On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    <> wrote:

    > I am using the time and date in my code using asctime () time(), and
    >localtime(). I know in POSIX they are obsolete but I am concerned with
    >c89-99. strftime() is suppoed to be the function used now. I get the time
    >and date just fine with the code I've written but I want to use the time to
    >seed a random number generator. I don't know if I want seconds from epoch or
    >what. Any input?
    >
    >Bill
    >


    srand((unsigned)time(NULL));
     
    Geoff, Nov 20, 2012
    #2
    1. Advertising

  3. "Bill Cunningham" <> writes:
    > I am using the time and date in my code using asctime () time(), and
    > localtime(). I know in POSIX they are obsolete but I am concerned with
    > c89-99. strftime() is suppoed to be the function used now. I get the time
    > and date just fine with the code I've written but I want to use the time to
    > seed a random number generator. I don't know if I want seconds from epoch or
    > what. Any input?


    strftime() has been in the C standard since 1989.

    A typical way to seed the standard random number generator is:

    srand(time(NULL));

    There are some drawbacks to this approach, discussed in the FAQ,
    <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2012
    #3
  4. Geoff <> writes:
    > On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    > <> wrote:
    >> I am using the time and date in my code using asctime () time(), and
    >>localtime(). I know in POSIX they are obsolete but I am concerned with
    >>c89-99. strftime() is suppoed to be the function used now. I get the time
    >>and date just fine with the code I've written but I want to use the time to
    >>seed a random number generator. I don't know if I want seconds from epoch or
    >>what. Any input?

    >
    > srand((unsigned)time(NULL));


    The cast is unnecessary. time() returns a value of type time_t,
    which is an arithmetic type; it will be implicitly converted to
    the appropriate type for the argument to srand() (which, yes,
    happens to be unsigned int).

    One could argue, I suppose, that the cast makes it more explicit,
    but I tend to view all casts with suspicion. Most of them are
    either unnecessary or dangerous.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2012
    #4
  5. Bill Cunningham

    Geoff Guest

    On Mon, 19 Nov 2012 16:51:21 -0800, Keith Thompson <>
    wrote:

    >Geoff <> writes:
    >> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    >> <> wrote:
    >>> I am using the time and date in my code using asctime () time(), and
    >>>localtime(). I know in POSIX they are obsolete but I am concerned with
    >>>c89-99. strftime() is suppoed to be the function used now. I get the time
    >>>and date just fine with the code I've written but I want to use the time to
    >>>seed a random number generator. I don't know if I want seconds from epoch or
    >>>what. Any input?

    >>
    >> srand((unsigned)time(NULL));

    >
    >The cast is unnecessary. time() returns a value of type time_t,
    >which is an arithmetic type; it will be implicitly converted to
    >the appropriate type for the argument to srand() (which, yes,
    >happens to be unsigned int).
    >
    >One could argue, I suppose, that the cast makes it more explicit,
    >but I tend to view all casts with suspicion. Most of them are
    >either unnecessary or dangerous.


    Then it looks like the c-faq needs revision:

    http://www.c-faq.com/lib/srand.html
     
    Geoff, Nov 20, 2012
    #5
  6. Keith Thompson wrote:
    > "Bill Cunningham" <> writes:
    >> I am using the time and date in my code using asctime () time(),
    >> and localtime(). I know in POSIX they are obsolete but I am
    >> concerned with c89-99. strftime() is suppoed to be the function used
    >> now. I get the time and date just fine with the code I've written
    >> but I want to use the time to seed a random number generator. I
    >> don't know if I want seconds from epoch or what. Any input?

    >
    > strftime() has been in the C standard since 1989.


    So if I just wanted to print the date and time I would use strftime() ?

    > A typical way to seed the standard random number generator is:
    >
    > srand(time(NULL));
    >
    > There are some drawbacks to this approach, discussed in the FAQ,
    > <http://www.c-faq.com/>.
     
    Bill Cunningham, Nov 20, 2012
    #6
  7. Keith Thompson wrote:
    > Geoff <> writes:
    >> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    >> <> wrote:
    >>> I am using the time and date in my code using asctime () time(),
    >>> and localtime(). I know in POSIX they are obsolete but I am
    >>> concerned with c89-99. strftime() is suppoed to be the function
    >>> used now. I get the time and date just fine with the code I've
    >>> written but I want to use the time to seed a random number
    >>> generator. I don't know if I want seconds from epoch or what. Any
    >>> input?

    >>
    >> srand((unsigned)time(NULL));

    >
    > The cast is unnecessary. time() returns a value of type time_t,
    > which is an arithmetic type; it will be implicitly converted to
    > the appropriate type for the argument to srand() (which, yes,
    > happens to be unsigned int).

    [snip]

    An arithmetic type? So what are you trying to say exactly in simple terms?
    This is an example of where I can pick up knowledge. Are you saying with
    "arithmetic types" casts aren't necessaary?

    Bill
     
    Bill Cunningham, Nov 20, 2012
    #7
  8. Bill Cunningham

    Nobody Guest

    On Mon, 19 Nov 2012 19:11:41 -0500, Bill Cunningham wrote:

    > I am using the time and date in my code using asctime () time(), and
    > localtime(). I know in POSIX they are obsolete but I am concerned with
    > c89-99. strftime() is suppoed to be the function used now.


    All of those are in C89 and C99. POSIX considers asctime() and ctime()
    deprecated in favour of strftime(), as the former use a hard-coded format
    with English day and month names.

    The others aren't deprecated, although the POSIX-specific re-entrant
    functions (gmtime_r, localtime_r) may be preferred over the standard ISO C
    equivalents (gmtime, localtime) for various reasons.

    > I get the time
    > and date just fine with the code I've written but I want to use the time
    > to seed a random number generator. I don't know if I want seconds from
    > epoch or what. Any input?


    The simple solution is srand(time(NULL)). However, time() only has a one
    second resolution, so consecutive invocations of the program may produce
    identical results. ISO C doesn't provide any mechanism to query the time
    with greater resolution; POSIX defines gettimeofday() with microsecond
    resolution and clock_gettime() with nanosecond resolution.

    For testing, it's often useful to be able to reproduce prior results, so
    it may be wise to allow a seed to be specified explicitly (e.g. via a
    command-line argument or environment variable), with seeding from the
    current time as a fall-back.
     
    Nobody, Nov 20, 2012
    #8
  9. "Bill Cunningham" <> writes:
    > Keith Thompson wrote:
    >> Geoff <> writes:
    >>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    >>> <> wrote:
    >>>> I am using the time and date in my code using asctime () time(),
    >>>> and localtime(). I know in POSIX they are obsolete but I am
    >>>> concerned with c89-99. strftime() is suppoed to be the function
    >>>> used now. I get the time and date just fine with the code I've
    >>>> written but I want to use the time to seed a random number
    >>>> generator. I don't know if I want seconds from epoch or what. Any
    >>>> input?
    >>>
    >>> srand((unsigned)time(NULL));

    >>
    >> The cast is unnecessary. time() returns a value of type time_t,
    >> which is an arithmetic type; it will be implicitly converted to
    >> the appropriate type for the argument to srand() (which, yes,
    >> happens to be unsigned int).

    > [snip]
    >
    > An arithmetic type? So what are you trying to say exactly in simple terms?
    > This is an example of where I can pick up knowledge. Are you saying with
    > "arithmetic types" casts aren't necessaary?


    An arithmetic type is either an integer type or a floating-point
    type (or a complex or imaginary type, but don't worry about those).
    N1570 6.2.5p18.

    If you assign a value of any arithmetic type to an object of
    any arithmetic type (or initialize it, or pass it as a function
    argument), the value will be implicitly converted to the target type.
    N1570 6.5.16.1p1, first bullet. There are also implicit conversions
    for other kinds of expressions, with more complicated rules; such
    conversions *usually* do the right thing, but sometimes you might
    need a cast to enforce a particular conversion.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2012
    #9
  10. Nobody <> writes:
    [...]
    > The simple solution is srand(time(NULL)). However, time() only has a one
    > second resolution, so consecutive invocations of the program may produce
    > identical results. ISO C doesn't provide any mechanism to query the time
    > with greater resolution; POSIX defines gettimeofday() with microsecond
    > resolution and clock_gettime() with nanosecond resolution.


    The language doesn't specify the resolution of the time() function
    or of type time_t, but yes, one second is very common.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2012
    #10
  11. In article <k8ehrs$ddf$>,
    "Bill Cunningham" <> wrote:

    > I am using the time and date in my code using asctime () time(), and
    > localtime(). I know in POSIX they are obsolete but I am concerned with
    > c89-99. strftime() is suppoed to be the function used now. I get the time
    > and date just fine with the code I've written but I want to use the time to
    > seed a random number generator. I don't know if I want seconds from epoch or
    > what. Any input?
    >
    > Bill


    You could consider using arc4random() instead (if your system supports
    it) and then not worry about having to seeding it.
     
    Mark Storkamp, Nov 20, 2012
    #11
  12. Keith Thompson wrote:
    > "Bill Cunningham" <> writes:
    >> Keith Thompson wrote:
    >>> Geoff <> writes:
    >>>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
    >>>> <> wrote:
    >>>>> I am using the time and date in my code using asctime ()
    >>>>> time(), and localtime(). I know in POSIX they are obsolete but I
    >>>>> am concerned with c89-99. strftime() is suppoed to be the function
    >>>>> used now. I get the time and date just fine with the code I've
    >>>>> written but I want to use the time to seed a random number
    >>>>> generator. I don't know if I want seconds from epoch or what. Any
    >>>>> input?
    >>>>
    >>>> srand((unsigned)time(NULL));
    >>>
    >>> The cast is unnecessary. time() returns a value of type time_t,
    >>> which is an arithmetic type; it will be implicitly converted to
    >>> the appropriate type for the argument to srand() (which, yes,
    >>> happens to be unsigned int).

    >> [snip]
    >>
    >> An arithmetic type? So what are you trying to say exactly in simple
    >> terms? This is an example of where I can pick up knowledge. Are you
    >> saying with "arithmetic types" casts aren't necessaary?

    >
    > An arithmetic type is either an integer type or a floating-point
    > type (or a complex or imaginary type, but don't worry about those).
    > N1570 6.2.5p18.
    >
    > If you assign a value of any arithmetic type to an object of
    > any arithmetic type (or initialize it, or pass it as a function
    > argument), the value will be implicitly converted to the target type.
    > N1570 6.5.16.1p1, first bullet. There are also implicit conversions
    > for other kinds of expressions, with more complicated rules; such
    > conversions *usually* do the right thing, but sometimes you might
    > need a cast to enforce a particular conversion.


    I have n1570.pdf. But it's still a little over my head. Do you know of
    an example page or anything that might explain this if you would happen to
    know. I might try looking up arithmetic types and see what I can find.

    Bill
     
    Bill Cunningham, Nov 20, 2012
    #12
  13. Mark Storkamp wrote:
    > In article <k8ehrs$ddf$>,


    > You could consider using arc4random() instead (if your system supports
    > it) and then not worry about having to seeding it.


    It evidently doesn't. I looked at everything defined and declared in my
    stdlib.h and it wasn't there. It didn't come up on my manpage either. I use
    fedora 17.

    Bill
     
    Bill Cunningham, Nov 21, 2012
    #13
  14. "Bill Cunningham" <> writes:
    > Mark Storkamp wrote:
    >> In article <k8ehrs$ddf$>,

    >
    >> You could consider using arc4random() instead (if your system supports
    >> it) and then not worry about having to seeding it.

    >
    > It evidently doesn't. I looked at everything defined and declared in my
    > stdlib.h and it wasn't there. It didn't come up on my manpage either. I use
    > fedora 17.


    arc4random() (which I'd never heard of before Mark mentioned it)
    is a BSD library function. On my system (Ubuntu), it's part of the
    "libbsd-dev" package.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 21, 2012
    #14
  15. On Nov 19, 9:07 pm, Nobody <> wrote:
    >
    > The simple solution is srand(time(NULL)). However, time() only has a one
    > second resolution, so consecutive invocations of the program may produce
    > identical results.


    Given an application for which, the standard rand family of functions
    is sufficient, why would one want to make consecutive invocations to
    srand?

    - Anand
     
    Anand Hariharan, Nov 21, 2012
    #15
  16. Bill Cunningham

    osmium Guest

    "Anand Hariharan" <> wrote in message
    news:...
    On Nov 19, 9:07 pm, Nobody <> wrote:
    >
    > The simple solution is srand(time(NULL)). However, time() only has a one
    > second resolution, so consecutive invocations of the program may produce
    > identical results.


    Given an application for which, the standard rand family of functions
    is sufficient, why would one want to make consecutive invocations to
    srand?

    Threading messed up by google's proprietary interface.

    Perhaps one might want to test his program before releasing it for general
    use?
     
    osmium, Nov 21, 2012
    #16
  17. Bill Cunningham

    James Kuyper Guest

    On 11/21/2012 11:30 AM, Anand Hariharan wrote:
    > On Nov 19, 9:07 pm, Nobody <> wrote:
    >>
    >> The simple solution is srand(time(NULL)). However, time() only has a one
    >> second resolution, so consecutive invocations of the program may produce
    >> identical results.

    >
    > Given an application for which, the standard rand family of functions
    > is sufficient, why would one want to make consecutive invocations to
    > srand?


    Keep in mind that he's talking about consecutive invocations of the
    entire program, not consecutive calls to srand() within a single program.

    I heard that there are servers out there that are capable of handling
    dozens or even hundreds of requests in a single second (I don't claim to
    have any personal experience with such things), and spawn a fresh copy
    of some program to handle each request. If srand(time(NULL)) were used
    in that program, it would generate the same exact sequence of random
    numbers for all requests received during the same second. For some
    possible uses, that would not be acceptable.
     
    James Kuyper, Nov 21, 2012
    #17
  18. Bill Cunningham

    Adam Wysocki Guest

    James Kuyper <> wrote:

    > If srand(time(NULL)) were used in that program, it would generate
    > the same exact sequence of random numbers for all requests received
    > during the same second.


    Maybe adding process ID to the seed calculation would do the job.

    Instead of:

    srand(time(NULL))

    Use:

    srand(time(NULL) ^ getpid())

    Or even:

    srand(time(NULL) ^ (getpid() << 16))

    AW
     
    Adam Wysocki, Nov 21, 2012
    #18
  19. d (Adam Wysocki) writes:
    > James Kuyper <> wrote:
    >> If srand(time(NULL)) were used in that program, it would generate
    >> the same exact sequence of random numbers for all requests received
    >> during the same second.

    >
    > Maybe adding process ID to the seed calculation would do the job.
    >
    > Instead of:
    >
    > srand(time(NULL))
    >
    > Use:
    >
    > srand(time(NULL) ^ getpid())
    >
    > Or even:
    >
    > srand(time(NULL) ^ (getpid() << 16))


    If it's worth going to that much trouble, it's probably worth using
    a better random number generator than rand() (one provided by the
    implementation but not specified by the C standard).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 21, 2012
    #19
  20. Keith Thompson wrote:

    > arc4random() (which I'd never heard of before Mark mentioned it)
    > is a BSD library function. On my system (Ubuntu), it's part of the
    > "libbsd-dev" package.


    OK I got it. There's several functions there I haven't sorted out yet.
    But arc4random() seems to work fine.

    Bill
     
    Bill Cunningham, Nov 21, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engsol
    Replies:
    2
    Views:
    996
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    511
    Magnus Lycka
    Aug 5, 2005
  3. Peter Hansen
    Replies:
    0
    Views:
    735
    Peter Hansen
    Feb 22, 2006
  4. Peter Hansen
    Replies:
    0
    Views:
    630
    Peter Hansen
    Feb 22, 2006
  5. flamesrock
    Replies:
    8
    Views:
    557
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page