Re: [Fwd: Re: surprising results with random()]

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [Fwd: Re: surprising results with random()]
Дата
Msg-id 28819.1235434562@sss.pgh.pa.us
обсуждение исходный текст
Ответ на [Fwd: Re: surprising results with random()]  (John R Pierce <pierce@hogranch.com>)
Список pgsql-general
John R Pierce <pierce@hogranch.com> writes:
> ries van Twisk wrote:
>> should the function not look like this???
>>
>> CREATE OR REPLACE VIEW test_view AS
>> SELECT
>> CASE
>> WHEN random() < .333333333 THEN '1'
>> WHEN random() < .5 THEN '2'
>> ELSE '3'
>> END AS test_value
>>
>> FROM client;

> actually, I'd think that should be .66666

Nah, ries is correct.  Think of it this way: one-third of the time the
first WHEN succeeds, and you get '1'.  In *half of the remaining cases*,
you want '2', so the second test should be against 0.5.

> but... wouldn't    floor(random() * 3.0) + 1  be a simpler way to do this?

Agreed...

            regards, tom lane

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

Предыдущее
От: "Brent Wood"
Дата:
Сообщение: Re: surprising results with random()
Следующее
От: raf
Дата:
Сообщение: Re: surprising results with random()