delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/11/16/14:14:26

Date: Mon, 16 Nov 1998 13:15:14 -0600
From: Eric Rudd <rudd AT cyberoptics DOT com>
Subject: Re: src/libc/ansi/stdlib/rand.c
To: djgpp-workers AT delorie DOT com
Message-id: <365079C2.5052B140@cyberoptics.com>
Organization: CyberOptics
MIME-version: 1.0
X-Mailer: Mozilla 4.05 [en] (Win95; U)
References: <199811161537 DOT KAA11282 AT indy DOT delorie DOT com>
Reply-To: djgpp-workers AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019