Search Postgresql Archives

Re: Random numbers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 






On 25 May 2013 18:14, Karel Riveron Escobar <kescobar@xxxxxxxxxxxxxxxxxx> 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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux