From owner-freebsd-current Sun Feb 2 5:17:54 2003 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1230337B405 for ; Sun, 2 Feb 2003 05:17:53 -0800 (PST) Received: from smtp04.iprimus.com.au (smtp04.iprimus.com.au [210.50.76.52]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4D4FF43F75 for ; Sun, 2 Feb 2003 05:17:48 -0800 (PST) (envelope-from tim@robbins.dropbear.id.au) Received: from smtp02.iprimus.net.au (210.50.76.70) by smtp04.iprimus.com.au (6.7.010) id 3E3608DE000FBBEC for current@FreeBSD.org; Mon, 3 Feb 2003 00:17:40 +1100 Received: from dilbert.robbins.dropbear.id.au ([203.134.135.129]) by smtp02.iprimus.net.au with Microsoft SMTPSVC(5.0.2195.5600); Mon, 3 Feb 2003 00:17:39 +1100 Received: from dilbert.robbins.dropbear.id.au (y1nff0j6npg4hofv@localhost [127.0.0.1]) by dilbert.robbins.dropbear.id.au (8.12.6/8.12.6) with ESMTP id h12DHaVY031341; Mon, 3 Feb 2003 00:17:37 +1100 (EST) (envelope-from tim@dilbert.robbins.dropbear.id.au) Received: (from tim@localhost) by dilbert.robbins.dropbear.id.au (8.12.6/8.12.6/Submit) id h12DHZIe031340; Mon, 3 Feb 2003 00:17:35 +1100 (EST) (envelope-from tim) Date: Mon, 3 Feb 2003 00:17:35 +1100 From: Tim Robbins To: "Andrey A. Chernov" Cc: Kris Kennaway , current@FreeBSD.org Subject: Re: rand() is broken Message-ID: <20030203001735.A30440@dilbert.robbins.dropbear.id.au> References: <20030202070644.GA9987@rot13.obsecurity.org> <20030202090422.GA59750@nagual.pp.ru> <20030202091106.GA72723@rot13.obsecurity.org> <20030202102621.GA60900@nagual.pp.ru> <20030202123035.GB62977@nagual.pp.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <20030202123035.GB62977@nagual.pp.ru>; from ache@nagual.pp.ru on Sun, Feb 02, 2003 at 03:30:35PM +0300 X-OriginalArrivalTime: 02 Feb 2003 13:17:40.0043 (UTC) FILETIME=[763241B0:01C2CABD] Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Feb 02, 2003 at 03:30:35PM +0300, Andrey A. Chernov wrote: > On Sun, Feb 02, 2003 at 13:26:21 +0300, Andrey A. Chernov wrote: > > > Workaround I find so far is something like that > > > > #define MASK 123459876 > > I found nothing better. Here is fix for 0 problem I plan to commit: > > --- stdlib/rand.c.old Sat Jan 4 20:39:19 2003 > +++ stdlib/rand.c Sun Feb 2 14:43:42 2003 > @@ -70,14 +70,18 @@ > * Park and Miller, Communications of the ACM, vol. 31, no. 10, > * October 1988, p. 1195. > */ > +#define SHIFT_MASK 123459876 > long hi, lo, x; > > - hi = *ctx / 127773; > - lo = *ctx % 127773; > + /* Can't be initialized with 0, so use shifting mask. */ > + x = *ctx ^ SHIFT_MASK; > + hi = x / 127773; > + lo = x % 127773; > x = 16807 * lo - 2836 * hi; > - if (x <= 0) > + if (x < 0) > x += 0x7fffffff; > - return ((*ctx = x) % ((u_long)RAND_MAX + 1)); > + *ctx = x ^ SHIFT_MASK; > + return (x % ((u_long)RAND_MAX + 1)); > #endif /* !USE_WEAK_SEEDING */ > } I believe that this change just moves the "bad" seed to 123459876; after calling srand() with that seed, each call to rand() returns 0. Tim To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message