Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #28439 > unrolled thread
| Started by | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| First post | 2012-11-19 19:11 -0500 |
| Last post | 2012-11-21 15:11 -0500 |
| Articles | 17 on this page of 37 — 14 participants |
Back to article view | Back to comp.lang.c
time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-19 19:11 -0500
Re: time Geoff <geoff@invalid.invalid> - 2012-11-19 16:26 -0800
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-19 16:51 -0800
Re: time Geoff <geoff@invalid.invalid> - 2012-11-19 17:01 -0800
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-19 20:07 -0500
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-19 23:54 -0800
Re: time pete <pfiland@mindspring.com> - 2012-11-20 07:31 -0500
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-20 17:09 -0500
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-19 16:30 -0800
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-19 20:04 -0500
Re: time Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-11-25 05:52 -0800
Re: time Nobody <nobody@nowhere.com> - 2012-11-20 03:07 +0000
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-19 23:58 -0800
Re: time Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-11-21 08:30 -0800
Re: time "osmium" <r124c4u102@comcast.net> - 2012-11-21 11:11 -0600
Re: time James Kuyper <jameskuyper@verizon.net> - 2012-11-21 12:22 -0500
Re: time gof@somewhere.invalid (Adam Wysocki) - 2012-11-21 19:09 +0000
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-21 12:01 -0800
Re: time gof@somewhere.invalid (Adam Wysocki) - 2012-11-23 08:15 +0000
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-21 15:16 -0500
Re: time "osmium" <r124c4u102@comcast.net> - 2012-11-21 14:23 -0600
Re: time Rosario1903 <Rosario@invalid.invalid> - 2012-11-22 10:25 +0100
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-21 12:30 -0800
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-21 18:24 -0500
Re: time Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-11-25 05:56 -0800
Re: time Nobody <nobody@nowhere.com> - 2012-11-22 02:00 +0000
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-21 19:57 -0800
Re: time gof@somewhere.invalid (Adam Wysocki) - 2012-11-23 08:31 +0000
Re: time army1987 <army1987@ask-for-it.invalid> - 2012-11-24 13:58 +0000
Re: time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-22 11:36 +0000
Re: time gof@somewhere.invalid (Adam Wysocki) - 2012-11-23 08:20 +0000
Re: time Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-23 14:31 +0000
Re: time Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-11-25 05:54 -0800
Re: time Mark Storkamp <mstorkamp@yahoo.com> - 2012-11-20 08:34 -0600
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-20 20:37 -0500
Re: time Keith Thompson <kst-u@mib.org> - 2012-11-20 20:25 -0800
Re: time "Bill Cunningham" <nospam@nspam.invalid> - 2012-11-21 15:11 -0500
Page 2 of 2 — ← Prev page 1 [2]
| From | "osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2012-11-21 14:23 -0600 |
| Message-ID | <ah4rhuF375fU1@mid.individual.net> |
| In reply to | #28540 |
"Bill Cunningham" wrote: > btw to stay on-topic here that caret and those << indicators. I've never > seen that in C except maybe headers. Are they preprocessing tokens? K&R has one of the best indexes I have ever seen in a technical book. It starts on p 263. But the whole point of writing a book is for people to *read* it. Try it some time.
[toc] | [prev] | [next] | [standalone]
| From | Rosario1903 <Rosario@invalid.invalid> |
|---|---|
| Date | 2012-11-22 10:25 +0100 |
| Message-ID | <qqrra81rn0a7q33lvg9io8m61sklqf5jp3@4ax.com> |
| In reply to | #28542 |
On Wed, 21 Nov 2012 14:23:26 -0600, "osmium" wrote: >K&R has one of the best indexes I have ever seen in a technical book. yes
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-11-21 12:30 -0800 |
| Message-ID | <lnzk2au2aq.fsf@nuthaus.mib.org> |
| In reply to | #28540 |
"Bill Cunningham" <nospam@nspam.invalid> writes:
> Adam Wysocki wrote:
>> 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))
>
> Is that really that random? As long as a process has fork() 'ed and the
> process is running it's pid isn't going to change is it until the system
> kills it.
You only call srand() once during the execution of the program.
> btw to stay on-topic here that caret and those << indicators. I've
> never seen that in C except maybe headers. Are they preprocessing tokens?
^ is the exclusive-or operator. << is the left-shift operator.
You'll find both in the index of the C standard and of K&R, which
you could have checked before wasting everyone's time by asking
about it here.
--
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"
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2012-11-21 18:24 -0500 |
| Message-ID | <k8jnq9$qmq$1@dont-email.me> |
| In reply to | #28543 |
Keith Thompson wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> Adam Wysocki wrote:
>>> 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))
>>
>> Is that really that random? As long as a process has fork() 'ed
>> and the process is running it's pid isn't going to change is it
>> until the system kills it.
>
> You only call srand() once during the execution of the program.
>
>> btw to stay on-topic here that caret and those <<
>> indicators. I've never seen that in C except maybe headers. Are they
>> preprocessing tokens?
>
> ^ is the exclusive-or operator. << is the left-shift operator.
> You'll find both in the index of the C standard and of K&R, which
> you could have checked before wasting everyone's time by asking
> about it here.
Oh yes that's right. I was thinking shells and posix in this discussion
about arc4random. The bit-wise operators ok I know what they are. Fell
asleep for a minute.
Bill
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-11-25 05:56 -0800 |
| Message-ID | <475f28e1-df29-461e-b063-c3c80e85c70a@f19g2000vbv.googlegroups.com> |
| In reply to | #28540 |
On Nov 21, 8:16 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote: > Adam Wysocki wrote: > > 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)) > > Is that really that random? As long as a process has fork() 'ed and the > process is running it's pid isn't going to change is it until the system > kills it.btw to stay on-topic here that caret and those << indicators. I've > never seen that in C except maybe headers. Are they preprocessing tokens? good grief. RTFM!
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-11-22 02:00 +0000 |
| Message-ID | <pan.2012.11.22.02.00.18.990000@nowhere.com> |
| In reply to | #28535 |
On Wed, 21 Nov 2012 19:09:18 +0000, Adam Wysocki 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. It would, and that's common. But there's no portable way to read the PID (if such a concept even exists on a given platform). The main issue is that time() appears to be about the only portable source of non-determinism available, and it's typically rather weak. On Wed, 21 Nov 2012 12:01:46 -0800, Keith Thompson wrote: > 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). But that doesn't do anything about the lack of non-determinism (entropy). A better PRNG may give you better random numbers, but without something other than time(), they'll be exactly the same random numbers as any other invocation which gets the same value from time().
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-11-21 19:57 -0800 |
| Message-ID | <lnr4nmthkt.fsf@nuthaus.mib.org> |
| In reply to | #28554 |
Nobody <nobody@nowhere.com> writes:
> On Wed, 21 Nov 2012 19:09:18 +0000, Adam Wysocki wrote:
[...]
> On Wed, 21 Nov 2012 12:01:46 -0800, Keith Thompson wrote:
>> 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).
>
> But that doesn't do anything about the lack of non-determinism (entropy).
> A better PRNG may give you better random numbers, but without something
> other than time(), they'll be exactly the same random numbers as any
> other invocation which gets the same value from time().
Some PRNGs are self-seeding. For example, if you can read
data from /dev/random, you'll get numbers that are pretty much
cryptographically secure (and /dev/urandom is nearly as good and
much faster).
--
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"
[toc] | [prev] | [next] | [standalone]
| From | gof@somewhere.invalid (Adam Wysocki) |
|---|---|
| Date | 2012-11-23 08:31 +0000 |
| Message-ID | <gof.1353658854.14351@somewhere.invalid> |
| In reply to | #28556 |
Keith Thompson <kst-u@mib.org> wrote: > Some PRNGs are self-seeding. For example, if you can read > data from /dev/random, you'll get numbers that are pretty much > cryptographically secure (and /dev/urandom is nearly as good and > much faster). Remember that /dev/random can quickly run out of numbers when entropy pool gets empty. AW
[toc] | [prev] | [next] | [standalone]
| From | army1987 <army1987@ask-for-it.invalid> |
|---|---|
| Date | 2012-11-24 13:58 +0000 |
| Message-ID | <k8qjqo$enc$1@dont-email.me> |
| In reply to | #28554 |
On Thu, 22 Nov 2012 02:00:19 +0000, Nobody wrote: > On Wed, 21 Nov 2012 19:09:18 +0000, Adam Wysocki 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. [...] > The main issue is that time() appears to be about the only portable > source of non-determinism available, and it's typically rather weak. Well, there's tmpnam()... -- [ T H I S S P A C E I S F O R R E N T ] Troppo poca cultura ci rende ignoranti, troppa ci rende folli. -- fathermckenzie di it.cultura.linguistica.italiano <http://xkcd.com/397/>
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-11-22 11:36 +0000 |
| Message-ID | <0.c09a7439420da7e67cb5.20121122113650GMT.87lidt50od.fsf@bsb.me.uk> |
| In reply to | #28535 |
gof@somewhere.invalid (Adam Wysocki) writes: > James Kuyper <jameskuyper@verizon.net> 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)) There a small thing you can do that might help on some (possibly theoretical) systems: srand(time(NULL) * 1000ULL); It's largely harmless on most systems since all you normally want is something that varies using the least significant bits of the time and 1000 times that is as good anything else, but on a system with a floating point time_t you get more of the variation. (The use of ULL is just to make it unlikely that the multiplication will overflow in the case of a signed integer time_t.) -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | gof@somewhere.invalid (Adam Wysocki) |
|---|---|
| Date | 2012-11-23 08:20 +0000 |
| Message-ID | <gof.1353658584.14036@somewhere.invalid> |
| In reply to | #28563 |
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: > but on a system with a floating point time_t you get more of the > variation. POSIX says that "time_t and clock_t shall be integer or real-floating types", so it is possible, but can you give an example of such system? I've never seen one. AW
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-11-23 14:31 +0000 |
| Message-ID | <0.659ec17a7602384a2aeb.20121123143123GMT.87vccw1jd0.fsf@bsb.me.uk> |
| In reply to | #28585 |
gof@somewhere.invalid (Adam Wysocki) writes: > Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: > >> but on a system with a floating point time_t you get more of the >> variation. > > POSIX says that "time_t and clock_t shall be integer or real-floating > types", so it is possible, but can you give an example of such system? No, hence my "possibly theoretical" remark. Maybe it should have been "probably theoretical". I see that there was a proposal to The Open Group to amend the its standard (AKA POSIX) to rule out all floating type for time_t. I don't know what happened to it -- I found the document tracking system rather confusing. > I've never seen one. Nor I. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-11-25 05:54 -0800 |
| Message-ID | <56dc730c-4485-4272-b942-15773743db16@r13g2000vbd.googlegroups.com> |
| In reply to | #28525 |
On Nov 21, 4:30 pm, Anand Hariharan <mailto.anand.hariha...@gmail.com> wrote: > On Nov 19, 9:07 pm, Nobody <nob...@nowhere.com> 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? I ran into trouble once when I started several identical executables at once. Thet all got the same "random" seed.
[toc] | [prev] | [next] | [standalone]
| From | Mark Storkamp <mstorkamp@yahoo.com> |
|---|---|
| Date | 2012-11-20 08:34 -0600 |
| Message-ID | <mstorkamp-8BA843.08343420112012@news.eternal-september.org> |
| In reply to | #28439 |
In article <k8ehrs$ddf$1@dont-email.me>, "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 You could consider using arc4random() instead (if your system supports it) and then not worry about having to seeding it.
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2012-11-20 20:37 -0500 |
| Message-ID | <k8hb97$evv$1@dont-email.me> |
| In reply to | #28471 |
Mark Storkamp wrote:
> In article <k8ehrs$ddf$1@dont-email.me>,
> 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
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-11-20 20:25 -0800 |
| Message-ID | <lnr4nnmvkd.fsf@nuthaus.mib.org> |
| In reply to | #28518 |
"Bill Cunningham" <nospam@nspam.invalid> writes:
> Mark Storkamp wrote:
>> In article <k8ehrs$ddf$1@dont-email.me>,
>
>> 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) 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"
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2012-11-21 15:11 -0500 |
| Message-ID | <k8jchk$foo$1@dont-email.me> |
| In reply to | #28521 |
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
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web