Re: [HACKERS] random() function produces wrong range - Mailing list pgsql-general

From Chris Jones
Subject Re: [HACKERS] random() function produces wrong range
Date
Msg-id x6ya2e2kqw.fsf@reddwarf.rightnowtech.com
Whole thread Raw
In response to Re: [HACKERS] random() function produces wrong range  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
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?"

pgsql-general by date:

Previous
From: Federico Sevilla III
Date:
Subject: PHP and PostgreSQL (was: Unions in views)
Next
From: "BJB"
Date:
Subject: Database security via a special ODBC connection possible?