Mail Archives: djgpp-workers/1998/11/16/14:14:26
DJ Delorie wrote:
> People who really need a repeatable sequence of numbers from rand()
> will need to use srand() anyway
I don't have my copy of C90 with me, but in the latest draft of C9x the
behavior is defined even when srand() is not called first:
7.20.2.2 The srand function
Synopsis
[#1]
#include <stdlib.h>
void srand(unsigned int seed);
Description
[#2] The srand function uses the argument as a seed for a
new sequence of pseudo-random numbers to be returned by
subsequent calls to rand. If srand is then called with the
same seed value, the sequence of pseudo-random numbers shall
be repeated. If rand is called before any calls to srand
have been made, the same sequence shall be generated as when
srand is first called with a seed value of 1.
> The only advantage to doing it the ANSI way is that people learn what
> will work correctly on other platforms.
>
> Does anyone else have an opinion on this?
Yes, I do. Some people (like me) go to the ANSI standard to find out what
a compiler should do, because we need our code to work on several
platforms. Adherence to the ANSI standard is the only way that this
portability can be achieved.
No matter how rand() and srand() are implemented, someone is always going
to be surprised. The surprised party ought to be the careless programmer
who hasn't read the C standard, rather than the careful programmer who has.
;-)
-Eric Rudd
rudd AT cyberoptics DOT com
- Raw text -