Re: consistent random order

From: "Aaron Bono" <postgresql(at)aranya(dot)com>
To: "Jeff Herrin" <jeff(at)alternateimage(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: consistent random order
Date: 2006-11-29 17:23:54
Message-ID: bf05e51c0611290923s2cfcd332l5ab20e74a606c858@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 11/29/06, Jeff Herrin <jeff(at)alternateimage(dot)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
==================================================================

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Kretschmer 2006-11-29 17:27:42 Re: consistent random order
Previous Message James Robinson 2006-11-29 17:08:12 Subselects in CHECK clause ...