Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Feb 2003 00:16:57 -0800 (PST)
From:      Paul Herman <pherman@frenchfries.net>
To:        Peter Jeremy <peterjeremy@optushome.com.au>
Cc:        FreeBSD Hackers <hackers@FreeBSD.ORG>
Subject:   Re: arc4random() range
Message-ID:  <20030218232210.T240-100000@mammoth.eat.frenchfries.net>
In-Reply-To: <20030219063646.GB62020@cirb503493.alcatel.com.au>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 19 Feb 2003, Peter Jeremy wrote:
> On Tue, Feb 18, 2003 at 06:22:37PM -0800, Paul Herman wrote:
> >
> > [...range of arc4random() is twice that of random()...]
>
> I see this as a major advantage of arc4random()

I see this as an advantage, too.  It's also produces random numbers
with a very long period.  It's also not too slow.  But that's not
what put the bee in my bonnet.  The disadvantage is that it doesn't
behave like random()/rand(), like I mistakingly assumed despite
reading the manpage.

Background: I probably spent an hour trying to figure out why my
monte carlo simulations (dealing with concordance of ranking
tables) were skewed until I narrowed it down to arc4random()
producing random numbers twice as big as random().  So, I was first
going to post to -doc about the lack of this fact in the manpage to
possibly spare others from a similar tribulation, but it occurred
to me to ask on -hackers what people thought about this break from
"tradition" first.

Unfortunately, this behavior is inconsistent with random(), perhaps
for good reason, but perhaps indeed the strongest argument above
any other for *not* changing arc4random() to a drop-in replacement
for random()/rand() is legacy: it's already been this way for quite
some time now and some code may depend on this behavior now.

If no one disagrees, I suggest an addition to the manpage along
the lines of:

  arc4random() returns random numbers in the range of 0 to
  (2**32)-1, and therefore has twice the range of RAND_MAX.

and if we are feeling really generous:

  EXAMPLES
  The following produces a drop-in replacement for the traditional
  random() and rand() functions using arc4random():
      #define arc4random31()   (arc4random() & 0x7FFFFFFF)

-Paul.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030218232210.T240-100000>