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?

asked Oct 2, 2014 at 18:09

2 Answers 2

6

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) ;
answered Oct 3, 2014 at 8:53
0
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.

answered Oct 2, 2014 at 19:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.