delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/11/16/03:20:22

Date: Mon, 16 Nov 1998 10:19:55 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: DJ Delorie <dj AT delorie DOT com>
cc: djgpp-workers AT delorie DOT com
Subject: Re: src/libc/ansi/stdlib/rand.c
In-Reply-To: <199811151915.OAA23094@indy.delorie.com>
Message-ID: <Pine.SUN.3.91.981116101933.7241F-100000@is>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

On Sun, 15 Nov 1998, DJ Delorie wrote:

> static unsigned long long next = 0;
> static int srand_called = 0;
>
> int
> rand(void)
> {
>   if (!srand_called)
>   {
>     unsigned int lsb, msb, tics;
>     outportb(0x43, 0x00);
>     lsb = inportb(0x40);
>     msb = inportb(0x40);
>     tics = _farpeekl(_dos_ds, 0x46c);
>     srand(lsb | (msb<<8) || (((long long)tics)<<16));
>   }

Doesn't this violate the ANSI Standard?  My references indicate that
it requires the implementation to ``behave as if the target
environment calls "srand(1)" at program startup.''  (Which also means
that `next' should start with 1, not 0.)

I think most people would expect `rand' to produce the same sequence
unless they called `srand', even if ANSI doesn't mandate it.

> void
> srand(unsigned seed)
> {
>   next = seed;
>   srand_called = 1;
>   rand();
>   rand();
>   rand();
> }

I think ANSI specifies that ``implementation shall behave as if no
library function calls the `rand' function.''  If I'm right, this
violates that requirement.  (Why are the calls to `rand' a good idea,
anyway?)

- Raw text -


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