Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Oct 1996 14:52:26 +0200 (MET DST)
From:      J Wunsch <j@uriah.heep.sax.de>
To:        freebsd-hackers@freebsd.org (FreeBSD hackers)
Cc:        jmacd@CS.Berkeley.EDU
Subject:   Re: rand() and random()
Message-ID:  <199610031252.OAA26494@uriah.heep.sax.de>
In-Reply-To: <199610022202.PAA04883@phaeton.artisoft.com> from Terry Lambert at "Oct 2, 96 03:02:42 pm"

next in thread | previous in thread | raw e-mail | index | archive | help
As Terry Lambert wrote:

> > I'm curious why rand() still exists.  Everyone knows its really
> > bad.  Why not just ditch it in favor of random()?
> 
> Because it is ANSI conrfmant (man rand).

That doesn't mean it would be impossible to do

int
rand(void)
{
	return (int)random();
}

The wrapping is needed since one function is declared to return int,
while the other returns long.  Other than this, both have the same
range for the result (RAND_MAX actually is (2**32)-1), so i fail to
see why random() should _not_ match the ANSI requirements.

(From random(3):)

     Unlike srand,  srandom() does not return the old seed; the reason for
     ^^^^^^^^^^^^
     this is that the amount of state information used is much more than a
     single word.  (Two other routines are provided to deal with restart-

The marked phrase is wrong.  srand() doesn't return the old seed
either, at least not in the ANSI version.  ISRT that it used to return
the old seed in obsolete implementations.

     ing/changing random number generators).  Like rand(3),  however, random()
     will by default produce a sequence of numbers that can be duplicated by
     calling srandom() with `1' as the seed.

...so this also matches the ANSI requirements.  There is absolutely no
reason for still supporting the old junk.  The extended interface of
srandom() doesn't hurt, since extensions are, of course, possible.

I vote for killing the old rand().  Too many things (in particular,
games) behave very stupid with it.

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199610031252.OAA26494>