Re: rand and srand
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Mon, 10 Mar 2008 02:30:22 +0530
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.
So what did you expect. Have you read the documentation for srand and
rand. The former "seeds" the pseudo-random number generator implemented
as rand, to start a new sequence of PRNs.
Also when I also try
RAND_MAX with srand from time to time.
That's not a very good idea. One common method is to use the return
value of the time function as the argument to srand.
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.
Well, implicit int return has been removed from the current standard,
but you'll find that almost all compilers will still tolerate code that
uses this feature. However it's always a good programming practise to
specify the return type for all your functions. It makes for *more*
readable code, not less.
Also rand returns an int value. The correct specifier for printf is %d,
not %u.
.
- References:
- rand and srand
- From: Bill Cunningham
- rand and srand
- Prev by Date: Re: rand and srand
- Next by Date: Re: all.h
- Previous by thread: Re: rand and srand
- Next by thread: Re: rand and srand
- Index(es):
Relevant Pages
|