Re: [HACKERS] Bug: random() can return 1.0

Lists: pgsql-hackerspgsql-patches
From: Andrew - Supernews <andrew+nonews(at)supernews(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Bug: random() can return 1.0
Date: 2006-01-19 22:40:41
Message-ID: slrndt05b9.1rvr.andrew+nonews@atlantis.supernews.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

src/backend/utils/adt/float.c:

/*
* drandom - returns a random number
*/
Datum
drandom(PG_FUNCTION_ARGS)
{
float8 result;

/* result 0.0-1.0 */
result = ((double) random()) / ((double) MAX_RANDOM_VALUE);

PG_RETURN_FLOAT8(result);
}

Whoever wrote this obviously did intend it to return values in [0.0,1.0]
but this makes it totally useless for generating uniform random ranges
in the usual way, since random() * N will return N with probability 2^-31.
The documentation is sufficiently imprecise about this to cause confusion
(seen in questions asked on the IRC channel), and the problem can't be
worked around at the application level without knowing the value of
MAX_RANDOM_VALUE in order to correct the range to [0.0,1.0).

--
Andrew, Supernews
http://www.supernews.com - individual and corporate NNTP services


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: andrew(at)supernews(dot)com
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug: random() can return 1.0
Date: 2006-01-19 22:49:35
Message-ID: 200601192249.k0JMnZr10157@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Andrew - Supernews wrote:
> src/backend/utils/adt/float.c:
>
> /*
> * drandom - returns a random number
> */
> Datum
> drandom(PG_FUNCTION_ARGS)
> {
> float8 result;
>
> /* result 0.0-1.0 */
> result = ((double) random()) / ((double) MAX_RANDOM_VALUE);
>
> PG_RETURN_FLOAT8(result);
> }
>
> Whoever wrote this obviously did intend it to return values in [0.0,1.0]
> but this makes it totally useless for generating uniform random ranges
> in the usual way, since random() * N will return N with probability 2^-31.
> The documentation is sufficiently imprecise about this to cause confusion
> (seen in questions asked on the IRC channel), and the problem can't be
> worked around at the application level without knowing the value of
> MAX_RANDOM_VALUE in order to correct the range to [0.0,1.0).

Because random returns a double, I think it is very possible that we
could return 1 due to rounding, and I see no way to avoid that. I think
re-running random if it returns 1 is likely to return even less random
values.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: andrew(at)supernews(dot)com, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug: random() can return 1.0
Date: 2006-01-19 23:26:09
Message-ID: 10560.1137713169@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> Because random returns a double, I think it is very possible that we
> could return 1 due to rounding,

Not unless your machine has a "double" type with less than 32 bits of
precision, which seems pretty unlikely. It'd be sufficient to do

/* result 0.0 <= x < 1.0 */
result = ((double) random()) / ((double) MAX_RANDOM_VALUE + 1.0);

regards, tom lane


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: andrew(at)supernews(dot)com, PostgreSQL-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: [HACKERS] Bug: random() can return 1.0
Date: 2006-02-01 17:23:54
Message-ID: 200602011723.k11HNsd29134@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> > Because random returns a double, I think it is very possible that we
> > could return 1 due to rounding,
>
> Not unless your machine has a "double" type with less than 32 bits of
> precision, which seems pretty unlikely. It'd be sufficient to do
>
> /* result 0.0 <= x < 1.0 */
> result = ((double) random()) / ((double) MAX_RANDOM_VALUE + 1.0);

Here is a patch that makes this change, and cleans up other
MAX_RANDOM_VALUE uses.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

Attachment Content-Type Size
unknown_filename text/plain 3.5 KB