time

B

Bill Cunningham

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
 
G

Geoff

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));
 
K

Keith Thompson

Bill Cunningham said:
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/>.
 
K

Keith Thompson

Geoff said:
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.
 
G

Geoff

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
 
B

Bill Cunningham

Keith said:
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
 
N

Nobody

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.
 
K

Keith Thompson

Bill Cunningham said:
Keith said:
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.
 
K

Keith Thompson

Nobody said:
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.

[...]
 
M

Mark Storkamp

Bill Cunningham said:
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.
 
B

Bill Cunningham

Keith said:
Bill Cunningham said:
Keith said:
On Mon, 19 Nov 2012 19:11:41 -0500, "Bill Cunningham"
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
 
B

Bill Cunningham

Mark said:
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
 
K

Keith Thompson

Bill Cunningham said:
Mark said:
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.
 
A

Anand Hariharan

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
 
O

osmium

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?
 
J

James Kuyper

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.
 
A

Adam Wysocki

James Kuyper said:
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
 
K

Keith Thompson

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).
 
B

Bill Cunningham

Keith said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top