cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/30/12:27:33

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: <Pine DOT SUN DOT 3 DOT 91 DOT 990730091120 DOT 25172H-100000 AT is>
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
<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 -


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