Mail Archives: djgpp/1999/07/30/12:27:33
On Fri, 30 Jul 1999 09:11:43 +0300 (IDT), Eli Zaretskii
<eliz AT is DOT elta DOT co DOT il> 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 <g>
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
- Raw text -