Re: consistent random order - Mailing list pgsql-sql

From Aaron Bono
Subject Re: consistent random order
Date
Msg-id bf05e51c0611290923s2cfcd332l5ab20e74a606c858@mail.gmail.com
Whole thread Raw
In response to consistent random order  (Jeff Herrin <jeff@alternateimage.com>)
Responses Re: consistent random order
List pgsql-sql
On 11/29/06, Jeff Herrin <jeff@alternateimage.com> wrote:

I am returning results ordered randomly using 'order by random()'. My issue has to do with page numbers in our web application. When I hit the 2nd page and retrieve results with an offset, ordering by random() isn't really what I want since I will often receive results that were on the 1st page (they get re-randomized).

I'm looking for a way to order in a controled random order. Maybe a UDF. Ideally I would need to do this:

ORDER BY myRandomUDF(1234)
or
ORDER BY myRandomUDF(2345)

Where the argument acts like a seed that always returns a consistent pseudo-random set. That way, when I get to the 2nd page, i know i'm getting the dataset back in the same order that I had on page 1, and the offset works like normal. Is this even realistically possible?


Why not create a random seed between 1 and the number of possibilities in your web application when a user first hits the site, store that in the session and then increment it by 1 (do a modulus to wrap the numbers back to 1) and just select with an offset equal to this number?  That way you get the first item chosen at random but the rest are sequential.  If you want this somewhat random, create a "random" ordering field on the table and sort by that so the pages are not sorted by name or id or whatever else it may normally sort by.

You could then take it further and use cookies so the next time that person comes to the site, the "random" page picks up where it left off.

Just an idea...

--
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================

pgsql-sql by date:

Previous
From: Jeff Herrin
Date:
Subject: consistent random order
Next
From: James Robinson
Date:
Subject: Subselects in CHECK clause ...