From owner-freebsd-current Tue Feb 4 6: 0:57 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 8AF7E37B405 for ; Tue, 4 Feb 2003 06:00:55 -0800 (PST) Received: from nagual.pp.ru (pobrecita.freebsd.ru [194.87.13.42]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4F0BF43FAF for ; Tue, 4 Feb 2003 06:00:54 -0800 (PST) (envelope-from ache@pobrecita.freebsd.ru) Received: from pobrecita.freebsd.ru (ache@localhost [127.0.0.1]) by nagual.pp.ru (8.12.6/8.12.6) with ESMTP id h14E0rd6093169; Tue, 4 Feb 2003 17:00:53 +0300 (MSK) (envelope-from ache@pobrecita.freebsd.ru) Received: (from ache@localhost) by pobrecita.freebsd.ru (8.12.6/8.12.6/Submit) id h14E0qMc093168; Tue, 4 Feb 2003 17:00:52 +0300 (MSK) (envelope-from ache) Date: Tue, 4 Feb 2003 17:00:52 +0300 From: "Andrey A. Chernov" To: Dag-Erling Smorgrav Cc: David Schultz , Kris Kennaway , current@FreeBSD.ORG Subject: Re: rand() is broken Message-ID: <20030204140052.GA93115@nagual.pp.ru> References: <20030202090422.GA59750@nagual.pp.ru> <20030203002639.GB44914@HAL9000.homeunix.com> <20030203100002.GA73386@nagual.pp.ru> <20030204054020.GA2447@HAL9000.homeunix.com> <20030204094659.GA87303@nagual.pp.ru> <20030204115237.GA6483@HAL9000.homeunix.com> <20030204131006.GB92301@nagual.pp.ru> <20030204131748.GA92510@nagual.pp.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.1i 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 Tue, Feb 04, 2003 at 14:43:57 +0100, Dag-Erling Smorgrav wrote: > I believe that's a feature. All linear congruence generator have a > fixed point. 0 is a far better fixed point than any other because it > is more "obviously unsuited" (for some values of "obviously") as a > seed value. (but see below) It is implementation details. rand() manpage says nothing about linear congruence generator and not suppose special meaning of 0 which means that application stuck at this point. We should not relay on that. > > The next bug is that there is % RAND_MAX + 1, not % RAND_MAX. > > No, using modulo 0x80000000 instead of modulo 0x7fffffff breaks the > algorithm. (but see below) Yes, but manpage says about range from 0 to RAND_MAX inclusive. It means that RAND_MAX clipping must be not part of algorithm and be applied only at later stage. > Changing RAND_MAX affects the algorithm in such a way that you should > not do so without giving serious thought to revising the algorithm Changing RAND_MAX should NOT affect algorithm at all, because RAND_MAX is not part of it. > All that being said, adding 1 to *ctx before returning it (see patch) > adresses both of your objections: a seed of 0 will not cause the LCG > to get stuck, and the result of rand() will range between 0 and > RAND_MAX inclusive. Adding +1 you break algorithm formulae badly from math point of view, something else then given formulae not allowed here. You can change 'a' parameter to anything you want, but not add something at the end. -- Andrey A. Chernov http://ache.pp.ru/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message