468,491 Members | 2,038 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

rand and srand

I am stumped on this one. I tried two methods and something just doesn't
seem right. I'll try my new syle.

#include <stdio.h>
#include <stdlib.h>

main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}

Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.

With main I was always told int was the default type and didn't need to be
declared. Main() has always worked. I hope this code is much more readable.

Bill
Mar 9 '08 #1
20 3456
Bill Cunningham wrote:

<about main>
The C99 standard wants and int.
Or a type compatible with int.
[...] So is there really a need to use srand ( ) ?
Yes. If you want your next pseudo-random sequence to be different and
not repeat.

Mar 9 '08 #2
On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<fa********@iridiumlinux.orgwrote in comp.lang.c:
Bill Cunningham wrote:
I am stumped on this one. I tried two methods and something just doesn't
seem right. I'll try my new syle.

#include <stdio.h>
#include <stdlib.h>

main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}

Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.

With main I was always told int was the default type and didn't need to be
declared. Main() has always worked. I hope this code is much more readable.

Bill
[snip]
As for your main(), I wasn't actually aware that such code was legal C.
Wherever did you get that idea? That code was perfectly legal in C up
until the 1999 update to the C language standard.
My guess is that it is actually void main(), because you never return
Now you're getting silly. Prior to C99, everyplace where it was legal
to define or declare something without an explicit type, it was
implicitly typed as int. Never as void.

main()

....prior to C99, was exactly identical to:

int main()

....and is illegal under C99 and later versions, as all declarators
must explicitly declare a type.
a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).
This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 9 '08 #3
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<fa********@iridiumlinux.orgwrote in comp.lang.c:
[...]
>a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).

This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.
No, C99 added a special-case rule that falling off the end of main()
does an implicit "return 0;"; see C99 5.1.2.2.3.

--
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 9 '08 #4
santosh said:
Bill Cunningham wrote:

<about main>
> The C99 standard wants and int.

Or a type compatible with int.
>[...] So is there really a need to use srand ( ) ?

Yes. If you want your next pseudo-random sequence to be different and
not repeat.
The way the OP was using srand(), the next pseudo-random sequence will not
be any different. I would explain further, but there's no point - it's all
in the FAQ, in the "Library Functions" section - see http://c-faq.com

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 9 '08 #5
Falcon Kirtaran <fa********@iridiumlinux.orgwrites:
...if you srand(1) in a program, the same sequence of random
numbers will be returned each time it is run on the same
implementation and platform.
If you don't call srand() at all, then the implementation behaves
as if srand(1) was called.
--
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 #6
On Sun, 09 Mar 2008 15:47:39 -0700, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<fa********@iridiumlinux.orgwrote in comp.lang.c:
[...]
a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).
This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.

No, C99 added a special-case rule that falling off the end of main()
does an implicit "return 0;"; see C99 5.1.2.2.3.
True, I overstated. Thanks for the correction.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 10 '08 #7
santosh wrote:
>
CBFalconer wrote:
http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf

What? Isn't this a "C99 draft"?
n1124 has to do with the next standard.
n1124 is not a draft of any published standard.

--
pete
Mar 10 '08 #8
santosh <sa*********@gmail.comwrites:
In any case my point standards.
This is now my favorite freudian slip. :)

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Mar 11 '08 #9
Keith Thompson wrote:
The "draft for C95" Chuck mentioned was in fact a post-C99 draft.
I was aware of that. I made my comment independently.
C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.
Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a Certain
Software Giant.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Mar 11 '08 #10
Mark McIntyre <ma**********@spamcop.netwrites:
Keith Thompson wrote:
>C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.

Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a
Certain Software Giant.
Every good C89 compiler warned about the use of implicit int if
set to a high-enough warning level, because use of implicit int
has never been good programming practice. That is quite
different from implicit int being declared obsolescent by the
standard. You don't expect that a compiler will warn only about
practices that the standard has declared obsolescent, do you?
--
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 11 '08 #11
Ben Pfaff wrote:
Mark McIntyre <ma**********@spamcop.netwrites:
>Keith Thompson wrote:
>>C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.
Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a
Certain Software Giant.

Every good C89 compiler warned about the use of implicit int if
set to a high-enough warning level, because use of implicit int
has never been good programming practice.
"Never" seems too strong. Just twenty years ago there
was no `void' keyword in the language; what would the good
programming practice of that time suggest as a declaration
of qsort(), say?

double qsort(); /* clearly wrong */
int qsort(); /* OK, but misleading */
qsort(); /* better, to my eye */

Observe that the latter two declarations meant exactly the
same thing, and that the third's is superior to the second
*because* its `int' is not explicit.

--
Er*********@sun.com
Mar 11 '08 #12
Eric Sosman <Er*********@sun.comwrites:
Ben Pfaff wrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>>Keith Thompson wrote:
C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.
Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a
Certain Software Giant.

Every good C89 compiler warned about the use of implicit int if
set to a high-enough warning level, because use of implicit int
has never been good programming practice.

"Never" seems too strong. [...]
Yes. I was thinking of this in the context of functions that
actually return a value. In that context, I believe that my
statement is correct.
--
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 11 '08 #13
Ben Pfaff wrote:
Mark McIntyre <ma**********@spamcop.netwrites:
>Keith Thompson wrote:
>>C95 (which appeared only as an amendment to C90) did not deprecate
implicit int.
Ok, fair enough. I had a recollection of supposed-c89 comppliant
compilers warning about it, but they were probably written by a
Certain Software Giant.

Every good C89 compiler warned about the use of implicit int if
set to a high-enough warning level, because use of implicit int
has never been good programming practice.
I don't think that's either true or relevant. IME not many compilers
warn about discarding the result of the f... family except at absurdly
high warning levels. Yet surelt that's bad programming practice too.
You don't expect that a compiler will warn only about
practices that the standard has declared obsolescent, do you?
Certainly I'd expect it warn about all sorts of things. I wouldn't
however expect it to warn about perfectly legal practices which some
people might consider distasteful or which offend their aesthetics or
principles.

I personally abhor arrays of function pointers, they're the devil's
spawn, and I insist on the braces-on-a-new-line style but I'm not going
to write a compiler that insults people who use a different layout.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Mar 12 '08 #14
In article <aS*****************@en-nntp-01.am2.easynews.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>I don't think that's either true or relevant. IME not many compilers
warn about discarding the result of the f... family except at absurdly
high warning levels. Yet surelt that's bad programming practice too.
There are lots of cases where it's perfectly reasonable not to test
the result of those functions. Perhaps you're going to check ferror()
later. Or if it's output to the terminal, there may well not be much
you can do about it - the likely cause of the error will also prevent
you from reporting it. So whether it's "bad programming practice"
depends on factors the compiler probably cannot determine.

-- Richard


--
:wq
Mar 13 '08 #15
santosh <sa*********@gmail.comwrites:
CBFalconer wrote:
[...]
>So I still don't have a proper description of c90 or C95, nor C89
(that standard is a copy of something I got from Dan Pop, which has
obvious holes in it). Bah.

Why not buy a copy from ANSI?
Last time I checked, ANSI no longer sells the C90 standard.

--
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 14 '08 #16
On Sun, 09 Mar 2008 17:20:22 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
<snip: implicit int>
...and is illegal under C99 and later versions, as all declarators
must explicitly declare a type.
Nit: declaration, or more specifically here function-definition, and
even more specifically declaration-specifiers, but not declarator.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Mar 24 '08 #17
Keith Thompson wrote:
santosh <sa*********@gmail.comwrites:
>CBFalconer wrote:
[...]
>>So I still don't have a proper description of c90 or C95, nor C89
(that standard is a copy of something I got from Dan Pop, which has
obvious holes in it). Bah.
Why not buy a copy from ANSI?

Last time I checked, ANSI no longer sells the C90 standard.
http://webstore.ansi.org/RecordDetai...u=AS+3955-1991

--
pete
Jun 27 '08 #18
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
>santosh <sa*********@gmail.comwrites:
>>CBFalconer wrote:
[...]
>>>So I still don't have a proper description of c90 or C95, nor C89
(that standard is a copy of something I got from Dan Pop, which has
obvious holes in it). Bah.
Why not buy a copy from ANSI?

Last time I checked, ANSI no longer sells the C90 standard.

http://webstore.ansi.org/RecordDetai...u=AS+3955-1991
Thanks, I was mistaken. (But $112 is more than I'd be willing to pay,
especially if the PDF is as bad as the copy I have.)

--
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"
Jun 27 '08 #19
On Thu, 08 May 2008 15:11:50 -0500, Morris Dovey wrote:
Keith Thompson wrote:
>Look again. They offer ISO/IEC 9899:1999 for $30. AS 3955-1991,
apparently a copy of the C90 standard, is $112.

I must be missing something. If it's identical to the ISO/IEC document,
then why not purchase the lower-priced document and save $82?
1990 != 1999. ISO/IEC 9899:1999 is C99, and that is available for $30. ISO/
IEC 9899:1990 is C90.
Jun 27 '08 #20
Keith Thompson wrote:
>
The $30 document is the C99 standard.
The $112 document is the C90 standard.
Does the price difference reflect the worth of C99's changes?

(With apologies to H.S. ;-)

--
Er*********@sun.com
Jun 27 '08 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by daniel kaplan | last post: by
11 posts views Thread by Tom McCallum | last post: by
36 posts views Thread by Profetas | last post: by
11 posts views Thread by Fernando Barsoba | last post: by
10 posts views Thread by Frank Silvermann | last post: by
26 posts views Thread by Gary Wessle | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.