Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
Thread:
Lists: pgsql-generalpgsql-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

pgsql-hackers by date

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

pgsql-general by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group