Skip to content

Instantly share code, notes, and snippets.

@ekho
Last active February 9, 2024 13:47
Show Gist options
  • Save ekho/6635570 to your computer and use it in GitHub Desktop.
Save ekho/6635570 to your computer and use it in GitHub Desktop.
Postgresql function for generating random integer array
CREATE OR REPLACE FUNCTION random_int_array(dim integer, min integer, max integer) RETURNS integer[] AS $BODY$
begin
return (select array_agg(round(random() * (max - min)) + min) from generate_series (0, dim));
end
$BODY$ LANGUAGE plpgsql;
-- usage example
select random_int_array(15, 6, 40);
-- return example
-- | random_int_array
-- | integer[]
-- -----------------------------------------------------
-- 1 | {26,20,13,37,21,25,38,39,36,21,31,6,32,16,22,10}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment