Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Feb 2003 17:37:17 +0000
From:      Bob Bishop <rb@gid.co.uk>
To:        kientzle@acm.org, Dag-Erling Smorgrav <des@ofug.org>
Cc:        phk@phk.freebsd.dk, Paul Herman <pherman@frenchfries.net>, Peter Jeremy <peterjeremy@optushome.com.au>, FreeBSD Hackers <hackers@FreeBSD.ORG>
Subject:   Re: arc4random() range
Message-ID:  <4.3.2.7.2.20030219173629.0213a848@gid.co.uk>
In-Reply-To: <3E53BE66.4030609@acm.org>
References:  <86890.1045646060@critter.freebsd.dk> <xzpsmukwqqb.fsf@flood.ping.uio.no>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi,

At 17:27 19/2/03, Tim Kientzle wrote:
>Dag-Erling Smorgrav wrote:
>
>>phk@phk.freebsd.dk writes:
>>
>>>Well, I'm right in principle but wrong in current practice, at
>>>the very least make it:
>>>
>>>#define arc4random31()   (arc4random() & RAND_MAX)
>>or rather
>>#define arc4random31()   (arc4random() % (RAND_MAX + 1))
>>to avoid relying on RAND_MAX being one less than a power of two.
>>DES
>
>
>Nope.  Because RAND_MAX is one less than a power of two,
>you run into integer overflow problems, which PHK's
>version avoids.

#define arc4random31()   (arc4random() % ((unsigned)RAND_MAX + 1))

--
Bob Bishop		    +44 (0)118 977 4017
rb@gid.co.uk		fax +44 (0)118 989 4254


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?4.3.2.7.2.20030219173629.0213a848>