PostgreSQL – How to Generate Sets of Random Integers

postgresqlpostgresql-9.3random

With this command I can generate 16384 random integers between 1 AND 200,000.

SELECT generate_series (1,16384),(random()*200000)::int AS id

I want to generate 10 sets of such integers. Each set must have an integer identifier, something like that:

1 | 135

1 | 1023

… end of first set of 16384 random numbers

2 | 15672

2 | 258732

… end of second set of 16384 random numbers

Is this possible with an SQL command, or should I write a function for that?

Best Answer

Would this be what you want?

SELECT 
   n, generate_series (1,16384), (random()*200000)::int AS id 
FROM  
   generate_series (1,10) AS x(n) ;

or the similar:

SELECT 
    n, i, (random()*200000)::int AS id 
FROM  
    generate_series (1,10) AS x(n) 
  CROSS JOIN
    generate_series (1,16384) AS y(i) ;