468,503 Members | 2,147 Online

# srand(time(NULL))

Regarding C95:
Is srand(time(NULL)); an effective solution for seeding rand(), or is
there any better approach?
Mar 10 '08 #1
31 6240
Ioannis Vranos wrote:
Regarding C95:
Is srand(time(NULL)); an effective solution for seeding rand(), or is
there any better approach?
There's some discussion in this area in section 13 of the C FAQ
(http://www.c-faq.com). Try Question 13.17

Mar 10 '08 #2
Ioannis Vranos wrote:
Regarding C95:
Is srand(time(NULL)); an effective solution for seeding rand(), or is
there any better approach?
"seeding a random number generator", many of which look helpful.
Mar 10 '08 #3
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
Is srand(time(NULL)); an effective solution for seeding rand(),
Unless your time_t is a floating point type and the current time is
larger than UINT_MAX (not likely, in particular the first part);
as long as you don't call it more than once every time_t tick;
and as long as your rand() is at least of semi-reasonable quality (i.e.,
successive seeds don't lead to similar series), it's fine.

Richard
Mar 10 '08 #4
Richard Bos wrote:
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
>Is srand(time(NULL)); an effective solution for seeding rand(),

Unless your time_t is a floating point type

The function prototype of srand() is

void srand(unsigned int seed);
Whatever time_t is, isn't there an implicit conversion to unsigned?
and the current time is
larger than UINT_MAX (not likely, in particular the first part);

If the value is larger than UINT_MAX, doesn't the argument seed just
wrap around?
Mar 10 '08 #5
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
Richard Bos wrote:
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
Is srand(time(NULL)); an effective solution for seeding rand(),
Unless your time_t is a floating point type

The function prototype of srand() is

void srand(unsigned int seed);

Whatever time_t is, isn't there an implicit conversion to unsigned?
Yes. But the conversion of a floating point value to an integral type
causes UB if that value, rounded down, would not fit into the integral
type - even if it's an unsigned type.
and the current time is
larger than UINT_MAX (not likely, in particular the first part);

If the value is larger than UINT_MAX, doesn't the argument seed just
wrap around?
You'd think so, but no, not on conversion from a floating point value.

At least, not in C90. I haven't looked up whether this remains true in
C99.

Richard
Mar 10 '08 #6
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
>Richard Bos wrote:
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
Is srand(time(NULL)); an effective solution for seeding rand(),
Unless your time_t is a floating point type
The function prototype of srand() is

void srand(unsigned int seed);

Whatever time_t is, isn't there an implicit conversion to unsigned?
Yes. But the conversion of a floating point value to an integral type
causes UB if that value, rounded down, would not fit into the integral
type - even if it's an unsigned type.
Additionally, a floating point time value may not be useful as a
random seed, for example if the time value is always between 0
and 1. (I am not aware of any systems that do this.)
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Mar 10 '08 #7
Keith Thompson said:

<snip>
>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,
Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Mar 10 '08 #8
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
<snip>
>>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,

Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>
NOOOOOOOOOOO!!!!!!!

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '08 #9
Keith Thompson said:

<snip>
Ben Pfaff's solution (treating the time_t result as an array of
unsigned char and hashing it) avoids all these problems,
As Ben would be the first to acknowledge, that was actually Lawrence
Kirby's solution.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Mar 10 '08 #10
Keith Thompson <ks***@mib.orgwrites:
he says:

Say, you're right. That's a last vestige of my PDP-11/V7/K&R C
days -- I thought I'd stamped those all out.
It's very odd to see the cast in the online FAQ, because the text
version that I keep on my local file system, which says it was
last modified February 7, 1999, omits the cast. Odd that it was
apparently (re?)introduced after that.

I should really update my local copy.
--
Ben Pfaff
http://benpfaff.org
Mar 10 '08 #11
On 10 Mar 2008 at 16:42, Richard Heathfield wrote:
Keith Thompson said:

<snip>
>>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,

Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.
The familiar Heathfield combination of minimal taste and maximal
polemic...

Mar 10 '08 #12
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:

<snip>
>>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,

Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>
K&R style is easily the most maintainable and easiest on the eye I have
ever used. It just "makes sense. header, body, footer. e.g

if(expr){
expr;
}
Mar 10 '08 #13
Richard wrote:
>
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:

<snip>
>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,
Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>

K&R style is easily the most maintainable and easiest on the eye I have
ever used. It just "makes sense. header, body, footer. e.g

if(expr){
expr;
}

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 10 '08 #14
Richard wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Keith Thompson said:

<snip>
>>I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed,
Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>

K&R style is easily the most maintainable and easiest on the eye I have
ever used. It just "makes sense. header, body, footer. e.g

if(expr){
expr;
}

LOL. I prefer the "more sense" to me

if(expr)
{
/* ... */
}

style. :-)
Mar 10 '08 #15
Ben Pfaff wrote:
Keith Thompson <ks***@mib.orgwrites:
he says:

Say, you're right. That's a last vestige of my PDP-11/V7/K&R C
days -- I thought I'd stamped those all out.

It's very odd to see the cast in the online FAQ, because the text
version that I keep on my local file system, which says it was
last modified February 7, 1999, omits the cast. Odd that it was
apparently (re?)introduced after that.

I should really update my local copy.

Perhaps he formatted his hard disk at some time, and his backup was a
bit old...
Mar 10 '08 #16
Ioannis Vranos wrote:
Richard wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>Keith Thompson said:

<snip>
I'd say [Henry Spencer's] 4th and 9th commandments are similarly
obsolete. The 8th
(which mandates K&R brace style) is something that I *wish*
everyone obeyed,
Fortunately, your wishing well is broken. K&R style is *dire*.

Cue the Brace Wars.

<snip>

K&R style is easily the most maintainable and easiest on the eye I
have ever used. It just "makes sense. header, body, footer. e.g

if(expr){
expr;
}

LOL. I prefer the "more sense" to me

if(expr)
{
/* ... */
}

style. :-)
This tends to waste vertical space. I tend to prefer your style for
function's opening and closing brace and use the K&R style for other
purposes, except for code blocks.

foo()
{
if (condition) {
/* ... */
}
else {
/* ... */
}

{
/* some code */
}

return;
}

Mar 10 '08 #17
santosh wrote:
>
>LOL. I prefer the "more sense" to me

if(expr)
{
/* ... */
}

style. :-)

This tends to waste vertical space.

More space sometimes means more readable.
I tend to prefer your style for
function's opening and closing brace and use the K&R style for other
purposes, except for code blocks.

foo()
{
if (condition) {
/* ... */
}
else {
/* ... */
}

{
/* some code */
}

return;
}

I like complete consistency myself. You know how this stuff goes,
everyone has his/her style.
Mar 10 '08 #18
santosh said:

<snip>
I prefer the "more sense" to me
>>
if(expr)
{
/* ... */
}

style. :-)

This tends to waste vertical space.
That's the disadvantage (for those who consider it so, of whom I am not
one). The advantage (for those who consider it so, of whom I am very
definitely one) is that the braces very obviously line up with each other,
visually reinforcing one's perception of the block scope.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Mar 10 '08 #19
Richard Heathfield wrote:
santosh said:

<snip>
I prefer the "more sense" to me
>
if(expr)
{
/* ... */
}

style. :-)
This tends to waste vertical space.

That's the disadvantage (for those who consider it so, of whom I am
not one).
Yeah, I was unaware that vertical space was in short supply.
The advantage (for those who consider it so, of whom I am
very definitely one) is that the braces very obviously line up with
each other, visually reinforcing one's perception of the block scope.
My personal preference is Whitesmiths style:

if(exp)
{
/* code */
}

The braces are part of the compound statement, and I like them at the
same indent level as the code.

Alas, the coding standard we use these days prefers your style, so I'm
shifting even my personal code to that style.

Brian
Mar 10 '08 #20
On Mon, 10 Mar 2008 22:44:55 +0000, Bill Cunningham wrote:
In C89 if I'm right there was no time.h or time_t type.
Both the <time.hheader and the time_t type are present in C89.
Mar 10 '08 #21
On Mon, 10 Mar 2008 15:33:58 -0600, Default User wrote
(in article <63*************@mid.individual.net>):
Richard Heathfield wrote:
>santosh said:

<snip>
>>I prefer the "more sense" to me

if(expr)
{
/* ... */
}

style. :-)

This tends to waste vertical space.

That's the disadvantage (for those who consider it so, of whom I am
not one).

Yeah, I was unaware that vertical space was in short supply.
Somewhere, in a dark whole, is someone still left on an 80x25 terminal.
:-)

Get a widescreen monitor that supports rotation, and run it vertically.
All the vertical space you need and then some.

--
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 10 '08 #22
>Is srand(time(NULL)); an effective solution for seeding rand(), or is
>there any better approach?
If you use this approach to generate random numbers for a game of chance
(such as, say, blackjack or craps) and make it available to the public
to play for real money, you're going to go bankrupt.

This is true for this method of seeding _any_ pseudo-random number
generator.

Mar 11 '08 #23
Gordon Burditt wrote:
>Is srand(time(NULL)); an effective solution for seeding rand(), or is
there any better approach?

If you use this approach to generate random numbers for a game of chance
(such as, say, blackjack or craps) and make it available to the public
to play for real money, you're going to go bankrupt.

This is true for this method of seeding _any_ pseudo-random number
generator.

Mar 11 '08 #24
Bill Cunningham wrote:

<snip>
Keith this wouldn've worked before c99. What did people do before C99?
In C89 if I'm right there was no time.h or time_t type. This answers
a previous question of mine.
No, both time.h and time_t were there in C89. And the same method that
Keith explained will work for all versions of standard C.

Mar 11 '08 #25
>>Is srand(time(NULL)); an effective solution for seeding rand(), or is
>>there any better approach?

If you use this approach to generate random numbers for a game of chance
(such as, say, blackjack or craps) and make it available to the public
to play for real money, you're going to go bankrupt.

This is true for this method of seeding _any_ pseudo-random number
generator.

Use specialized (and probably expensive) hardware based on physics
that is currently believed to be true random due to quantum effects.
This includes things like detection of radioactive decay and thermal
noise of a reverse-biased diode. Some processors (certain models
of Intel Pentium) have this built in, but I'm not sure how good it
is.

Also, use a seed much bigger than 32 bits. And you probably want to
use the time down to picoseconds.

Keystroke timing probably doesn't alone provide enough randomness
for online gambling with real money.

Mar 11 '08 #26
Randy Howard wrote:
On Mon, 10 Mar 2008 15:33:58 -0600, Default User wrote
(in article <63*************@mid.individual.net>):
Richard Heathfield wrote:
santosh said:

<snip>

I prefer the "more sense" to me

if(expr)
{
/* ... */
}

style. :-)

This tends to waste vertical space.

That's the disadvantage (for those who consider it so, of whom I am
not one).
Yeah, I was unaware that vertical space was in short supply.

Somewhere, in a dark whole, is someone still left on an 80x25
terminal. :-)
I still use them when I telnet in places. That just seems the "right"
size for that.

Brian
Mar 11 '08 #27
Randy Howard wrote:
Somewhere, in a dark whole, is someone still left on an 80x25 terminal.
:-)
Hmm. I see a light up there. Hellooo?

The link below leads to my latest project. Scroll to the bottom
of the page...

:-D

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/Projects/Monitor/
Mar 11 '08 #28
Morris Dovey wrote:
Randy Howard wrote:
>Somewhere, in a dark whole, is someone still left on an 80x25
terminal.
:-)

Hmm. I see a light up there. Hellooo?

The link below leads to my latest project. Scroll to the bottom
of the page...
Well, for my personal programming I prefer 80x25 text mode, because I
find that it minimises eye strain. I also prefer text mode due to long
years with DOS and UNIX (without X).

Most workplaces here are still full of old CRTs with terrible flicker,
set inappropriately to something like 1024x768, minimising the refresh
rate.

Mar 11 '08 #29
In article <63*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>Somewhere, in a dark whole, is someone still left on an 80x25
terminal. :-)
>I still use them when I telnet in places. That just seems the "right"
size for that.
It was too small 30 years ago and it's too small now.

-- Richard
--
:wq
Mar 11 '08 #30
Randy Howard <ra*********@FOOverizonBAR.netwrote:
On Mon, 10 Mar 2008 15:33:58 -0600, Default User wrote
Richard Heathfield wrote:
santosh said:

This tends to waste vertical space.

That's the disadvantage (for those who consider it so, of whom I am
not one).
Yeah, I was unaware that vertical space was in short supply.

Somewhere, in a dark whole, is someone still left on an 80x25 terminal.
:-)
Less. I've programmed on a handheld (a Psion Revo, to be precise),
though not in C.

Anyway, real debugging should be done on paper, not on the screen.

Richard
Mar 12 '08 #31
Richard Tobin wrote:
In article <63*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>>Somewhere, in a dark whole, is someone still left on an 80x25
terminal. :-)
>I still use them when I telnet in places. That just seems the "right"
size for that.

It was too small 30 years ago and it's too small now.
Pshaw. My screen is currently covered in 80x25 terminal windows. Nothing
wrong with 'em, perfect for doing 6 things at once.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>