Skip to content

Instantly share code, notes, and snippets.

@palpha
Created January 9, 2014 12:11
Show Gist options
  • Save palpha/8333162 to your computer and use it in GitHub Desktop.
Save palpha/8333162 to your computer and use it in GitHub Desktop.
PostgreSQL: generate a random series with configurable start, max and step.
with recursive t (n) as (
with base as (
with settings as (
select
10000 as max
, array[1000,2000] as start_range
, array[10,20] as step_range
)
select
(random() * (start_range[2] - start_range[1]) + start_range[1])::int as start
, step_range
, max
from settings
)
select start from base
union all
select n + (random() * (step_range[2] - step_range[1]) + step_range[1])::int
from t cross join base
where n < max
)
select n
from t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment