Date: Wed, 7 Jun 2017 12:00:15 -0700 From: Steve Kargl <sgk@troutmask.apl.washington.edu> To: "Littlefield, Tyler" <tyler@tysdomain.com> Cc: freebsd-hackers@freebsd.org Subject: Re: RAND_MAX is power of 2 Message-ID: <20170607190015.GA6134@troutmask.apl.washington.edu> In-Reply-To: <9efa6e4a-3166-58a8-99a3-02e920366f39@tysdomain.com> References: <9efa6e4a-3166-58a8-99a3-02e920366f39@tysdomain.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jun 07, 2017 at 02:34:48PM -0400, Littlefield, Tyler wrote: > All: > > I'm trying to find another solution to this and port this server to > FreeBSD. So far the only thing holding me up is that they want RAND_MAX > to be a power of 2, and I don't want to just change things without > knowing what I'm changing. Is there a reason for this? svn log -r 252608 /usr/src/include/stdlib.h r252608 | ache | 2013-07-03 14:21:54 -0700 (Wed, 03 Jul 2013) | 15 lines 1) POSIX requires rand(3) return values to be in the [0, RAND_MAX] range, but ACM formula we use have internal state (and return value) in the [1, 0x7ffffffe] range, so our RAND_MAX (0x7fffffff) is never reached because it is off by one, zero is not reached too. Correct both RAND_MAX and rand(3) return value, shifting last one to the 0 by 1 subtracted, resulting POSIXed [0, 0x7ffffffd(=new RAND_MAX)] range. 2) Add a checks for not overflowing on too big seeds. It may happens on the machines, where sizeof(unsigned int) > 32 bits. Reviewed by: bde [1] MFC after: 2 weeks -- Steve 20170425 https://www.youtube.com/watch?v=VWUpyCsUKR4 20161221 https://www.youtube.com/watch?v=IbCHE-hONow
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20170607190015.GA6134>