Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #28439 > unrolled thread

time

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2012-11-19 19:11 -0500
Last post2012-11-21 15:11 -0500
Articles 20 on this page of 37 — 14 participants

Back to article view | Back to comp.lang.c


Contents

  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 1 of 2  [1] 2  Next page →


#28439 — time

From"Bill Cunningham" <nospam@nspam.invalid>
Date2012-11-19 19:11 -0500
Subjecttime
Message-ID<k8ehrs$ddf$1@dont-email.me>
    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

[toc] | [next] | [standalone]


#28440

FromGeoff <geoff@invalid.invalid>
Date2012-11-19 16:26 -0800
Message-ID<4hjla8hdbe4ft1vjnnv6ppdqd08odelkbg@4ax.com>
In reply to#28439
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));

[toc] | [prev] | [next] | [standalone]


#28444

FromKeith Thompson <kst-u@mib.org>
Date2012-11-19 16:51 -0800
Message-ID<lnd2z9f65y.fsf@nuthaus.mib.org>
In reply to#28440
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"

[toc] | [prev] | [next] | [standalone]


#28445

FromGeoff <geoff@invalid.invalid>
Date2012-11-19 17:01 -0800
Message-ID<ehlla8hp3tctjo2q3hesr7mcf1n2fhlc10@4ax.com>
In reply to#28444
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

[toc] | [prev] | [next] | [standalone]


#28447

From"Bill Cunningham" <nospam@nspam.invalid>
Date2012-11-19 20:07 -0500
Message-ID<k8el52$u6c$1@dont-email.me>
In reply to#28444
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

[toc] | [prev] | [next] | [standalone]


#28454

FromKeith Thompson <kst-u@mib.org>
Date2012-11-19 23:54 -0800
Message-ID<lnd2z8r9oe.fsf@nuthaus.mib.org>
In reply to#28447
"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"

[toc] | [prev] | [next] | [standalone]


#28462

Frompete <pfiland@mindspring.com>
Date2012-11-20 07:31 -0500
Message-ID<50AB7811.7712@mindspring.com>
In reply to#28454
Keith Thompson wrote:
> 
> "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.

The condition of "*usually* do the right thing" 
also exists 
for arithmetic to arithmetic conversions.

n1570
6.3 Conversions
6.3.1.4 Real floating and integer
1
    If the value of the integral part 
    cannot be represented by the integer type, 
    the behavior is undefined.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#28497

From"Bill Cunningham" <nospam@nspam.invalid>
Date2012-11-20 17:09 -0500
Message-ID<k8gv37$64g$1@dont-email.me>
In reply to#28454
Keith Thompson wrote:
> "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.

    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

[toc] | [prev] | [next] | [standalone]


#28441

FromKeith Thompson <kst-u@mib.org>
Date2012-11-19 16:30 -0800
Message-ID<lnsj85f753.fsf@nuthaus.mib.org>
In reply to#28439
"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"

[toc] | [prev] | [next] | [standalone]


#28446

From"Bill Cunningham" <nospam@nspam.invalid>
Date2012-11-19 20:04 -0500
Message-ID<k8ekvd$ta2$1@dont-email.me>
In reply to#28441
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/>. 

[toc] | [prev] | [next] | [standalone]


#28642

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2012-11-25 05:52 -0800
Message-ID<d77aa883-25f7-41e1-9a16-d585d89fb9b8@ib4g2000vbb.googlegroups.com>
In reply to#28446
On Nov 20, 1:04 am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> Keith Thompson wrote:
> > "Bill Cunningham" <nos...@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() ?

what does the man page for strftime say?

[toc] | [prev] | [next] | [standalone]


#28449

FromNobody <nobody@nowhere.com>
Date2012-11-20 03:07 +0000
Message-ID<pan.2012.11.20.03.07.29.506000@nowhere.com>
In reply to#28439
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.

[toc] | [prev] | [next] | [standalone]


#28455

FromKeith Thompson <kst-u@mib.org>
Date2012-11-19 23:58 -0800
Message-ID<ln8v9wr9ie.fsf@nuthaus.mib.org>
In reply to#28449
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"

[toc] | [prev] | [next] | [standalone]


#28525

FromAnand Hariharan <mailto.anand.hariharan@gmail.com>
Date2012-11-21 08:30 -0800
Message-ID<8dd71a20-5caa-4e4a-8c1f-5c7d84b56bd2@h9g2000yqd.googlegroups.com>
In reply to#28449
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

[toc] | [prev] | [next] | [standalone]


#28529

From"osmium" <r124c4u102@comcast.net>
Date2012-11-21 11:11 -0600
Message-ID<ah4g9nFiu5U1@mid.individual.net>
In reply to#28525
"Anand Hariharan" <mailto.anand.hariharan@gmail.com> wrote in message 
news:8dd71a20-5caa-4e4a-8c1f-5c7d84b56bd2@h9g2000yqd.googlegroups.com...
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? 

[toc] | [prev] | [next] | [standalone]


#28530

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-11-21 12:22 -0500
Message-ID<50AD0DC6.5090505@verizon.net>
In reply to#28525
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.

[toc] | [prev] | [next] | [standalone]


#28535

Fromgof@somewhere.invalid (Adam Wysocki)
Date2012-11-21 19:09 +0000
Message-ID<gof.1353524440.12609@somewhere.invalid>
In reply to#28530
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))

AW

[toc] | [prev] | [next] | [standalone]


#28537

FromKeith Thompson <kst-u@mib.org>
Date2012-11-21 12:01 -0800
Message-ID<ln7gpevi6t.fsf@nuthaus.mib.org>
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))

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-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]


#28584

Fromgof@somewhere.invalid (Adam Wysocki)
Date2012-11-23 08:15 +0000
Message-ID<gof.1353658464.13945@somewhere.invalid>
In reply to#28537
Keith Thompson <kst-u@mib.org> wrote:

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

It might work when the only goal is to have different random seed in 
processes forked many times per second.

AW

[toc] | [prev] | [next] | [standalone]


#28540

From"Bill Cunningham" <nospam@nspam.invalid>
Date2012-11-21 15:16 -0500
Message-ID<k8jcq2$hmf$1@dont-email.me>
In reply to#28535
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?

Bill

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web