Re: [HACKERS] random() function produces wrong range

Chris Jones <cjones@rightnowtech.com>

From: Chris Jones <cjones@rightnowtech.com>
To: pgsql-general <pgsql-general@postgresql.org>
Date: 2000-08-03T19:43:51Z
Lists: pgsql-hackers
Tom Lane <tgl@sss.phg.pa.us> writes:

> Thomas Lockhart <lockhart@alumni.caltech.edu> writes:
> > The Linux man pages indicate that the behavior and underlying
> > implementation of random() and rand() are the same (so I just picked
> > one).
> 
> Ah, well, there's your problem.  Whoever did this part of the library
> on Linux took shortcuts.  On older-line systems, rand() is a
> considerably older and crummier generator than random().  It would
> definitely not be a wise decision to use rand() instead.
> 
> I believe using random() is the right thing.  The portability bug here
> is the assumption that RAND_MAX applies to random() (or is even defined;
> none of the man pages I've looked at so far mention it).  But all the
> machines say that the output of random() is 31 bits, so INT_MAX should
> work.

On an i386 machine, certainly; but not on an Alpha or a Sparc.
Probably safer to use (2**31)-1, which is what my (NetBSD) man page
says.

FWIW, I believe random(3) running on NetBSD/alpha, for example, will
return a 31-bit result.

Chris

-- 
---------------------------------------------------- cjones@rightnowtech.com
Chris Jones
           System Administrator, RightNow Technologies
"Is this going to be a stand-up programming session, sir, or another bug hunt?"