Re: Random numbers

Поиск
Список
Период
Сортировка
От Karel Riveron Escobar
Тема Re: Random numbers
Дата
Msg-id 79562a00-002a-492b-af67-eb4de3e3363e@ucimail4.uci.cu
обсуждение исходный текст
Ответ на Re: Random numbers  (Szymon Guz <mabewlun@gmail.com>)
Ответы Re: Random numbers  (Szymon Guz <mabewlun@gmail.com>)
Список pgsql-general
How would be if I would want to generate values among 3 and 5?

Regards, Karel Riverón
Student Scientific Council
Informatics Science University


From: "Szymon Guz" <mabewlun@gmail.com>
To: "Ian Lawrence Barwick" <barwick@gmail.com>
Cc: "bricklen" <bricklen@gmail.com>, "Karel Riveron Escobar" <kescobar@estudiantes.uci.cu>, pgsql-general@postgresql.org
Sent: Saturday, May 25, 2013 12:10:52 PM
Subject: Re: [GENERAL] Random numbers

On 25 May 2013 17:56, Ian Lawrence Barwick <barwick@gmail.com> wrote:
2013/5/26 bricklen <bricklen@gmail.com>:
>
> On Sat, May 25, 2013 at 8:45 AM, Karel Riveron Escobar
> <kescobar@estudiantes.uci.cu> wrote:
>>
>> I want to generate random numbers in Pl/pgSQL. How can I do this?
>> To be more specific, I have to generate random numbers among 0 and 5.
>
>
> One way:
> select n from unnest(ARRAY[0,1,2,3,4,5]) n order by random() limit 1;

somewhat shorter:
 SELECT (random() * 5)::INT


I'd rather use something like:

SELECT floor(random()*6)::INT

as this gives uniform results distribution.

Compare the two below queries, in the first you have twice less results for 0 and 5 than for the rest numbers.

WITH x AS (SELECT (random()*5)::INT r FROM generate_series(1,1000*1000)) SELECT r, count(*) FROM x GROUP BY r ORDER BY r ; 

WITH x AS (SELECT floor(random()*6)::INT r FROM generate_series(1,1000*1000)) SELECT r, count(*) FROM x GROUP BY r ORDER BY r ; 

regards
Szymon



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

Предыдущее
От: Szymon Guz
Дата:
Сообщение: Re: Random numbers
Следующее
От: Szymon Guz
Дата:
Сообщение: Re: Random numbers