Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Feb 2003 17:36:47 +1100
From:      Peter Jeremy <peterjeremy@optushome.com.au>
To:        Paul Herman <pherman@frenchfries.net>
Cc:        FreeBSD Hackers <hackers@FreeBSD.ORG>
Subject:   Re: arc4random() range
Message-ID:  <20030219063646.GB62020@cirb503493.alcatel.com.au>
In-Reply-To: <20030218180736.L240-100000@mammoth.eat.frenchfries.net>
References:  <20030219013247.GA10910@x-anthony.com> <20030218180736.L240-100000@mammoth.eat.frenchfries.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Feb 18, 2003 at 06:22:37PM -0800, Paul Herman wrote:
>On Tue, 18 Feb 2003, Anthony Schneider wrote:
>
>> an issue of arc4random return u_int32_t and rand*
>> returning int (ie unsigned vs signed)?
>
>Nope, casting arc4random() to int or casting random() to unsigned
>int won't solve the problem.  The problem still is that
>arc4random()'s range is twice that of it's friends'.

I see this as a major advantage of arc4random() - if I want 32-bit
random numbers I don't have to call random() twice and merge the
results.  I've never understood why random() was specified to return
a '0' in the MSB.

If you insist on a 31-bit random int, try
	(int)(arc4random() & 0x7fffffff)

Peter

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?20030219063646.GB62020>