delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/12/07:08:58

Date: Wed, 12 Nov 1997 14:07:52 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Paul Derbyshire <ao950 AT FreeNet DOT Carleton DOT CA>
cc: djgpp AT delorie DOT com
Subject: Re: rand() or random()
In-Reply-To: <64153n$ipi@freenet-news.carleton.ca>
Message-ID: <Pine.SUN.3.91.971112140658.20379H-100000@is>
MIME-Version: 1.0

On 8 Nov 1997, Paul Derbyshire wrote:

> If rand() is ANSI but GCC's algorithm for rand() sucks, while its
> algorithm for random() is great, why not just use the better algorithm for
> both rand() and random(), while making sure the rand() arguments and
> return values have the same ranges and semantics as ANSI requires?

`random' wasn't written for DJGPP project.  It was taken from the BSD
Unix library which is in public domain (and therefore free), but is
copyrighted by the Regents of the University of California.  Somebody
with a good understanding in the legal aspects should look at the
copyright and tell whether the function's name can be changed.  DJ should 
be one person who should answer that.

> What exactly is the difference between the functions except the names?

Everything.  `rand' is a simple linear-congruential generator.  It
doesn't ``suck'', it's actually quite good and has a full 2^32 cycle,
but its low bits are not so ``random'', so "rand()%3" doesn't produce
a random 2-bit number.

`random' is much more complex and sophisticated, and produces much
more random low bits.  I cannot explain here all its intricacies (this
issue took D. Knuth several hundreds of pages to explain), but you can
look into the code, which is commented to a large extent, and see for
yourself.

- Raw text -


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