Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > time

Reply
 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-20-2012
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


 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      11-20-2012
On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
<(E-Mail Removed)> 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));
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2012
"Bill Cunningham" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2012
Geoff <(E-Mail Removed)> writes:
> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
> <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      11-20-2012
On Mon, 19 Nov 2012 16:51:21 -0800, Keith Thompson <(E-Mail Removed)>
wrote:

>Geoff <(E-Mail Removed)> writes:
>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>> <(E-Mail Removed)> 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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-20-2012
Keith Thompson wrote:
> "Bill Cunningham" <(E-Mail Removed)> 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/>.



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-20-2012
Keith Thompson wrote:
> Geoff <(E-Mail Removed)> writes:
>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>> <(E-Mail Removed)> 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


 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      11-20-2012
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2012
"Bill Cunningham" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> Geoff <(E-Mail Removed)> writes:
>>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>>> <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-20-2012
Nobody <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Re: interpreting the fractional portion of time.clock() vs time.time(0measurements Peter Hansen Python 0 02-22-2006 02:02 PM
Re: interpreting the fractional portion of time.clock() vs time.time()measurements Peter Hansen Python 0 02-22-2006 12:03 AM
time.clock() or time.time() peterbe@gmail.com Python 8 08-05-2005 01:51 PM
delta time = time stop - time start engsol Python 2 01-26-2004 12:06 PM



Advertisments