Date: Sun, 15 Nov 1998 14:15:44 -0500 (EST) Message-Id: <199811151915.OAA23094@indy.delorie.com> From: DJ Delorie To: djgpp-workers AT delorie DOT com Subject: src/libc/ansi/stdlib/rand.c Reply-To: djgpp-workers AT delorie DOT com * new multiplier from K.B. Williams * auto-initialize if the user forgets to Comments? /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ #include #include #include #include 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)); } next = next * 6364136223846793005LL + 1; /* was: next = next * 0x5deece66dLL + 11; */ return (int)((next >> 16) & RAND_MAX); } void srand(unsigned seed) { next = seed; srand_called = 1; rand(); rand(); rand(); }