delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/12/00:12:53

Message-ID: <37B0C018.FBD2286@lycosmail.com>
Date: Tue, 10 Aug 1999 20:13:12 -0400
From: Adam Schrotenboer <ajschrotenboer AT lycosmail DOT com>
X-Mailer: Mozilla 4.6 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com, Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
Subject: Re: how about "more" random ?
References: <Pine DOT SUN DOT 3 DOT 91 DOT 990730091120 DOT 25172H-100000 AT is>
Reply-To: djgpp AT delorie DOT com


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?

Perhaps I should not say that it was wrong, yet it still could be.
Especially if the intent is to cast to int.

Let's say there is no real difference, mathematically. I would say
(IMNSHO) that my version is cleaner, and looks nicer. Perhaps it does
not. I will not belabor the point of looking nicer.

For cleaner, I think that variable numbers (here the 100) should be at
the end, to make it easier to find, and to change.

Also, If I may refer you to my response to a previous thread, RE: --
Random Question, dated 7/29/99, you may see the reason.

Also I just noticed that I made a mistake in that message:

Rather than:

for an int
int number = (int)((rand()/RAND_MAX)*(high - low + 1));

for a float
int number = (float)((rand()/RAND_MAX)*(high - low));

it should have been

for an int
int number = (int)((rand()/RAND_MAX)*(high - low + 1))+low;

for a float
int number = (float)((rand()/RAND_MAX)*(high - low))+low;

Still, maybe the difference is immaterial, but clean code is important
not only for others to read your code, but also for yourself to fix
bugs, add features, and maybe to copy/rewrite code from your previous
programs into a new program.

PS, sorry It has taken me so long to reply, I've been very busy w/ work,
Linux, and getting ready for school.

- Raw text -


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