3

The following will generate the same random number:

SELECT 
setseed(0.5), 
generate_series(1, 3) as id, 
round(random()::decimal, 2) as random_number;

Which outputs something along the lines of :

 setseed | id | random_number 
---------+----+---------------
         |  1 |          0.25
         |  2 |          0.25
         |  3 |          0.25

I would like something such as

 setseed | id | random_number 
---------+----+---------------
         |  1 |          0.11
         |  2 |          0.92
         |  3 |          0.38

And to be able to regenerate the above every time, as a result of setting a random seed.

Basically: I don't want the numbers in the random_number field to be a single random number repeated across all rows, I'd like for each row to be a random number

1 Answer 1

5

Run it as two statements:

select setseed(0.5);
select g.id, 
       round(random()::decimal, 2) as random_number
from generate_series(1, 3) as g(id);

Alternatively it seems to work if you use setseed() in a scalar sub-query:

select (select setseed(0.5)),
       g.id, 
       round(random()::decimal, 2) as random_number
from generate_series(1, 3) as g(id);
1
  • ahh, ok. Thanks!
    – baxx
    Commented Jan 4, 2023 at 1:23

Your Answer

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

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