1

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?

5

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) ;
4

You can do something like this:

SELECT ((generate_series-1)/16384)::int+1 as series, (random()*200000)::int as rand
    from generate_series(1,10*16384);

This does the sampling with replacement. If it needs to be done without replacement, that is quite a bit hard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.