From owner-freebsd-hackers Wed Feb 19 1:22: 3 2003 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3C29C37B401 for ; Wed, 19 Feb 2003 01:22:02 -0800 (PST) Received: from HAL9000.homeunix.com (12-233-57-224.client.attbi.com [12.233.57.224]) by mx1.FreeBSD.org (Postfix) with ESMTP id 846EA43F85 for ; Wed, 19 Feb 2003 01:22:01 -0800 (PST) (envelope-from dschultz@uclink.Berkeley.EDU) Received: from HAL9000.homeunix.com (localhost [127.0.0.1]) by HAL9000.homeunix.com (8.12.6/8.12.5) with ESMTP id h1J9M09l028608; Wed, 19 Feb 2003 01:22:00 -0800 (PST) (envelope-from dschultz@uclink.Berkeley.EDU) Received: (from das@localhost) by HAL9000.homeunix.com (8.12.6/8.12.5/Submit) id h1J9M0KX028607; Wed, 19 Feb 2003 01:22:00 -0800 (PST) (envelope-from dschultz@uclink.Berkeley.EDU) Date: Wed, 19 Feb 2003 01:22:00 -0800 From: David Schultz To: Paul Herman Cc: phk@phk.freebsd.dk, Peter Jeremy , FreeBSD Hackers Subject: Re: arc4random() range Message-ID: <20030219092200.GA26914@HAL9000.homeunix.com> Mail-Followup-To: Paul Herman , phk@phk.freebsd.dk, Peter Jeremy , FreeBSD Hackers References: <77946.1045642969@critter.freebsd.dk> <20030219003725.W240-100000@mammoth.eat.frenchfries.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030219003725.W240-100000@mammoth.eat.frenchfries.net> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Thus spake Paul Herman : > On Wed, 19 Feb 2003 phk@phk.freebsd.dk wrote: > > > In message <20030218232210.T240-100000@mammoth.eat.frenchfries.net>, Paul Herma > > n writes: > > > > > arc4random() returns random numbers in the range of 0 to > > > (2**32)-1, and therefore has twice the range of RAND_MAX. > > > > Good. > > > > > EXAMPLES > > > The following produces a drop-in replacement for the traditional > > > random() and rand() functions using arc4random(): > > > #define arc4random31() (arc4random() & 0x7FFFFFFF) > > > > Not good. Only true on 32 bit archs. > > Heh? > > random(3) produces numbers between 0 and RAND_MAX on my alpha. > random(3) produces numbers between 0 and RAND_MAX on my i386. > (arc4random() & 0x7FFFFFFF) produces numbers between 0 and RAND_MAX on my alpha. > (arc4random() & 0x7FFFFFFF) produces numbers between 0 and RAND_MAX on my i386. Your code should work fine, although it assumes RAND_MAX is 2^31-1, rather than using the symbolic constant. But I think if people were really interested in using arc4random() as a drop-in replacement for random(), they would have already written a libc function to mimic random(). Just documenting the range of arc4random() should be sufficient. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message