Re: random() function produces wrong range
Christopher Masto <chris@netmonger.net>
From: Christopher Masto <chris@netmonger.net>
To: Tom Lane <tgl@sss.pgh.pa.us>, roberts@panix.com
Cc: Thomas Swan <tswan@olemiss.edu>,
Malcolm Beattie <mbeattie@sable.ox.ac.uk>, Thomas Lockhart <lockhart@alumni.caltech.edu>, pgsql-hackers@postgresql.org, pgsql-general@postgresql.org
Date: 2000-08-04T18:28:28Z
Lists: 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/