Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > time

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


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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-21-2012
"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) kst- <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
 
Anand Hariharan
Guest
Posts: n/a
 
      11-21-2012
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?

- Anand
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      11-21-2012

"Anand Hariharan" <> wrote in message
news:8dd71a20-5caa-4e4a-8c1f-...
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?

Threading messed up by google's proprietary interface.

Perhaps one might want to test his program before releasing it for general
use?


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-21-2012
On 11/21/2012 11:30 AM, Anand Hariharan 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?


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.

 
Reply With Quote
 
Adam Wysocki
Guest
Posts: n/a
 
      11-21-2012
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-21-2012
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) kst- <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
 
Bill Cunningham
Guest
Posts: n/a
 
      11-21-2012
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


 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57