From: | Szymon Guz <mabewlun(at)gmail(dot)com> |
---|---|
To: | Karel Riveron Escobar <kescobar(at)estudiantes(dot)uci(dot)cu> |
Cc: | bricklen <bricklen(at)gmail(dot)com>, PostgreSQL <pgsql-general(at)postgresql(dot)org>, Ian Lawrence Barwick <barwick(at)gmail(dot)com> |
Subject: | Re: Random numbers |
Date: | 2013-05-25 16:19:51 |
Message-ID: | CAFjNrYt-kErJOv7TcSE3S+8CKOFo8NQwiJ6WNBEvG-ZSfB6fPA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 25 May 2013 18:14, Karel Riveron Escobar <kescobar(at)estudiantes(dot)uci(dot)cu>wrote:
> How would be if I would want to generate values among 3 and 5?
>
>
>
Hi Karel,
try something like:
SELECT floor(3 + random()*(5-3+1))::INT
Or generally:
CREATE OR REPLACE FUNCTION
random_range(INTEGER, INTEGER) RETURNS INTEGER
AS $$
SELECT floor(($1 + ($2 - $1 + 1) * random()))::INTEGER;
$$ LANGUAGE SQL;
and then:
SELECT random_range(3,5);
regards
Szymon
From | Date | Subject | |
---|---|---|---|
Next Message | Moshe Jacobson | 2013-05-25 19:37:04 | 9.1: Slow to add range check on indexed column |
Previous Message | Karel Riveron Escobar | 2013-05-25 16:14:33 | Re: Random numbers |