Re: Bug: random() can return 1.0

Поиск
Список
Период
Сортировка
От Bruce Momjian
Тема Re: Bug: random() can return 1.0
Дата
Msg-id 200601192249.k0JMnZr10157@candle.pha.pa.us
обсуждение исходный текст
Ответ на Bug: random() can return 1.0  (Andrew - Supernews <andrew+nonews@supernews.com>)
Ответы Re: Bug: random() can return 1.0  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-hackers
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@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073
 


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Thomas Hallgren
Дата:
Сообщение: PostgreSQL and shared memory.
Следующее
От: Jeremy Drake
Дата:
Сообщение: Re: No heap lookups on index