From: horst DOT kraemer AT snafu DOT de (Horst Kraemer) Newsgroups: comp.os.msdos.djgpp Subject: Re: how about "more" random ? Date: Fri, 30 Jul 1999 10:31:43 GMT Organization: [Posted via] Interactive Networx Lines: 39 Message-ID: <37a166f1.169247036@news.snafu.de> References: NNTP-Posting-Host: n65-177.berlin.snafu.de X-Newsreader: Forte Free Agent 1.11/32.235 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Fri, 30 Jul 1999 09:11:43 +0300 (IDT), Eli Zaretskii wrote: > > On Thu, 29 Jul 1999, Adam Schrotenboer wrote: > > > > x = ((double) rand ()) * 100 / RAND_MAX; > > > > Not exactly the correct method (IIRC), but close: > > > > x = ((double) rand ()/RAND_MAX) * 100; > > Care to explain what is the difference? The difference is that both methods are wrong A correct formula is int x = (double)rand()*100 / (RAND_MAX+1.0) ; or int x = rand()/(RAND_MAX+1.0)*100 ; And to be precise, both versions yield exactly the same result if RAND_MAX+1 is a power of 2 (which is the case for DJGPP) and 'double' is implemented as an IEEE double which is the case for DJGPP, too. (With the incorrect RAND_MAX instead of RAND_MAX+1.0, the result will not be necessarily identical because the division by RAND_MAX is not a clean SHIFT of the 2-based exponent of the double value but a regular division). Regards Horst