Date: Sun, 1 Aug 1999 11:29:44 +0300 (IDT) From: Eli Zaretskii X-Sender: eliz AT is To: Horst Kraemer cc: djgpp AT delorie DOT com Subject: Re: how about "more" random ? In-Reply-To: <37a166f1.169247036@news.snafu.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Fri, 30 Jul 1999, Horst Kraemer 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) ; This will never generate 100, only numbers that are less that 100. Depending on the case in point, this might or might not be what the original poster wanted. > (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). Since the results are meant to be random, I don't see why this effect should matter, it simply introduces some more random bits near the LSB.