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

From: Chris Jones <cjones(at)rightnowtech(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: [HACKERS] random() function produces wrong range
Date: 2000-08-03 19:43:51
Message-ID: x6ya2e2kqw.fsf@reddwarf.rightnowtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Tom Lane <tgl(at)sss(dot)phg(dot)pa(dot)us> writes:

> Thomas Lockhart <lockhart(at)alumni(dot)caltech(dot)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(at)rightnowtech(dot)com
Chris Jones
System Administrator, RightNow Technologies
"Is this going to be a stand-up programming session, sir, or another bug hunt?"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message BJB 2000-08-03 21:53:54 Database security via a special ODBC connection possible?
Previous Message Federico Sevilla III 2000-08-03 18:10:42 PHP and PostgreSQL (was: Unions in views)

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2000-08-03 19:57:47 Re: Toasting more system-table columns
Previous Message Mike Mascari 2000-08-03 19:36:50 Re: comparing rows