Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   time (http://www.velocityreviews.com/forums/t954647-time.html)

Bill Cunningham 11-20-2012 12:11 AM

time
 
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



Geoff 11-20-2012 12:26 AM

Re: time
 
On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
<nospam@nspam.invalid> 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));

Keith Thompson 11-20-2012 12:30 AM

Re: time
 
"Bill Cunningham" <nospam@nspam.invalid> 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) kst-u@mib.org <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 11-20-2012 12:51 AM

Re: time
 
Geoff <geoff@invalid.invalid> writes:
> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
> <nospam@nspam.invalid> 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) kst-u@mib.org <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"

Geoff 11-20-2012 01:01 AM

Re: time
 
On Mon, 19 Nov 2012 16:51:21 -0800, Keith Thompson <kst-u@mib.org>
wrote:

>Geoff <geoff@invalid.invalid> writes:
>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>> <nospam@nspam.invalid> 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

Bill Cunningham 11-20-2012 01:04 AM

Re: time
 
Keith Thompson wrote:
> "Bill Cunningham" <nospam@nspam.invalid> 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 11-20-2012 01:07 AM

Re: time
 
Keith Thompson wrote:
> Geoff <geoff@invalid.invalid> writes:
>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>> <nospam@nspam.invalid> 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



Nobody 11-20-2012 03:07 AM

Re: time
 
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.


Keith Thompson 11-20-2012 07:54 AM

Re: time
 
"Bill Cunningham" <nospam@nspam.invalid> writes:
> Keith Thompson wrote:
>> Geoff <geoff@invalid.invalid> writes:
>>> On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
>>> <nospam@nspam.invalid> 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) kst-u@mib.org <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 11-20-2012 07:58 AM

Re: time
 
Nobody <nobody@nowhere.com> 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) kst-u@mib.org <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"


All times are GMT. The time now is 01:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.