Mail Archives: djgpp-workers/1998/11/16/03:20:22
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 -