Re: random() function produces wrong range - Mailing list pgsql-hackers

From Christopher Masto
Subject Re: random() function produces wrong range
Date
Msg-id 20000804142828.B18784@netmonger.net
Whole thread Raw
In response to Re: [GENERAL] Re: random() function produces wrong range  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Differences between int8 and int4 as pkeys and fkeys  (Thomas Swan <tswan@olemiss.edu>)
List pgsql-hackers
On Thu, Aug 03, 2000 at 11:45:39AM -0400, Tom Lane wrote:
> Actually the odds are far better than that.  If the range is 2^31-1
> then only about 2^-16th of the outputs should be less than 2^15.
> So ten probes gives you a failure probability of about 2^-160 not
> 2^-10.

It occurs to me that Perl has to provide a portable rand function, so
I looked at how its Configure script works.  It's pretty much what
you've been discussing.  First it checks for a couple of possible
random functions (preferring drand48(), then random(), then bitching
and using rand()).

int main()
{
        register int i;
        register unsigned long tmp;
        register unsigned long max = 0L;

        for (i = 1000; i; i--) {
                tmp = (unsigned long) $randfunc();
                if (tmp > max) max = tmp;
        }
        for (i = 0; max; i++)
                max /= 2;
        printf("%d\n",i);
}

Oh well.
--
Christopher Masto         Senior Network Monkey      NetMonger Communications
chris@netmonger.net        info@netmonger.net        http://www.netmonger.net

Free yourself, free your machine, free the daemon -- http://www.freebsd.org/

pgsql-hackers by date:

Previous
From: Christopher Masto
Date:
Subject: Re: comparing rows
Next
From: Thomas Swan
Date:
Subject: Differences between int8 and int4 as pkeys and fkeys