Generate a random number of non duplicated random number in [0, 1001] through a loop

痴心易碎 提交于 2019-12-04 03:39:21

If I understand right

  • You need a random number (1 to 8) of random numbers.
  • The random numbers span 1 to 1001.
  • The random numbers need to be unique. None shall appear more than once.

CREATE OR REPLACE FUNCTION x.unique_rand_1001()
RETURNS SETOF integer AS
$body$
DECLARE
    nrnr    int := trunc(random()*7+1);  -- number of numbers
BEGIN

    RETURN QUERY
    SELECT (1000 * random())::integer + 1
    FROM   generate_series(1, nrnr*2)
    GROUP  BY 1
    LIMIT  nrnr;

END;
$body$ LANGUAGE plpgsql VOLATILE;

Call:

SELECT x.unique_rand_1001();

Numbers are made unique by the GROUP BY. I generate twice as many numbers as needed to provide enough numbers in case duplicates are removed. With the given dimensions of the task (max. 8 of 1001 numbers) it is astronomically unlikely that not enough numbers remain. Worst case scenario: viewer numbers are returned.

I wouldn't approach the problem that way in PostgreSQL. Something along these lines seems to make more sense.

-- Returns a random integer in the interval [n, m].
CREATE OR REPLACE FUNCTION random_integer(integer, integer)
  RETURNS integer AS
$BODY$
   select trunc( $1 + (($2*random()) ))::integer
$BODY$
  LANGUAGE sql VOLATILE

Then to select a single random integer between 1 and 1000,

select random_integer(1, 1000);

To select 100 random integers between 1 and 1000,

select random_integer(1, 1000)
from generate_series(1,100);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!