From: Martin Stromberg Message-Id: <200005191159.NAA08580@lws256.lu.erisoft.se> Subject: Re: rand() in libc To: djgpp-workers AT delorie DOT com Date: Fri, 19 May 2000 13:59:06 +0200 (MET DST) In-Reply-To: <200004271315.JAA03708@delorie.com> from "Dieter Buerssner" at Apr 27, 2000 04:20:23 PM X-Mailer: ELM [version 2.5 PL3] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp-workers AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp-workers AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk Dieter said: > On 27 Apr 00, at 14:05, Martin Stromberg wrote: > > In particular I wonder how the ones generating doubles > > (drand48(), erand48()) behave, as I improvised most on them. > > My manpages don't document the method, that should be > used, to create a [0,1[ double. If I understand your code correctly, > you "mix" the bits of the three state variables. > > return > (b16s2,b16s1,b16s0,b15s2,b15s1,b15s0, ..., b1s2,b1s1,b1s0)/2^48. > > (b16s2 is the most significant bit of s2, and so on ...) > > A more natural approach seems to me to just do > > s2*2^-16 + s1*2^-32 + s0*2^-48; > > Doing this with floating point arithmetics will always yield "exact" > results, no rounding will be involved with binary floating point > representation. > > Reason for my suggestion: s0 is not very random at all (period 2^16). > With the "mixing method", information of s0 will creep into the most > significant digits of the returned double. Ok, how doI easily say 2^-16, 2^-32 and 2^-48 to the compiler? Is it ok to use pow(2, -16) etc. (efficiency)? Right, MartinS