delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/01/15:51:31

Date: Sun, 1 Aug 1999 11:29:44 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Horst Kraemer <horst DOT kraemer AT snafu DOT de>
cc: djgpp AT delorie DOT com
Subject: Re: how about "more" random ?
In-Reply-To: <37a166f1.169247036@news.snafu.de>
Message-ID: <Pine.SUN.3.91.990801112643.20304K-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 <g>
> 
> 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.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019