Re: select random order by random - Mailing list pgsql-general

From brian
Subject Re: select random order by random
Date
Msg-id 4729FA18.4000703@zijn-digital.com
Whole thread Raw
In response to select random order by random  (piotr_sobolewski <piotr_sobolewski@o2.pl>)
List pgsql-general
piotr_sobolewski wrote:
> Dear sirs,
>
> I was very surprised when I executed such SQL query (under PostgreSQL 8.2):
> select random() from generate_series(1, 10) order by random();
>
> I thought I would receive ten random numbers in random order. But I received
> ten random numbers sorted numerically:
>       random
> -------------------
>  0.102324520237744
>   0.17704638838768
>  0.533014383167028
>   0.60182224214077
>  0.644065519794822
>  0.750732169486582
>  0.821376844774932
>   0.88221683120355
>  0.889879426918924
>  0.924697323236614
> (10 rows)
>
> I don't understand - why the result is like that? It seems like in each row
> both random()s were giving the same result. Why is it like that? What caused
> it?
>

Your query specifically requested that the result be ordered by the
column "random" in the result set (the default ordering direction being
ASC). Your query is semantically identical to:

SELECT random() AS foo FROM generate_series(1, 10) ORDER BY foo ASC;

I should think that you would get a better result if you dropped the
ORDER BY clause.

brian

pgsql-general by date:

Previous
From: Jeff MacDonald
Date:
Subject: Solaris 10, mod_auth_pgsql2
Next
From: "Scott Marlowe"
Date:
Subject: Re: select random order by random