Random Pagination

Hi Experts,

Is there way to do Random pagination?

I’m not with order, or sort basis. just random pagination

regards,

Bala

Bala wrote:

Hi Experts,

Is there way to do Random pagination?

I’m not with order, or sort basis. just random pagination

regards,

Bala

hmm. Here goes my rambling noob attempts: 3 ways -

You can get records from the db in random order with :order =>
‘random()’, but this is a bit inefficient. There’s a plugin called
‘random_finders’ which is more efficent, read about it here:
http://source.collectiveidea.com/public/rails/plugins/random_finders/

The problem with the pagination system i use (for you) is that it
reorders and re-gets the data for each page when it’s required. IE, if
i want page 5 (where pages start at 1), and i have 15 items per page,
then i do an ordered query and ask for items 60 to 74:

return Article.find(
  :all,
  :order => "points DESC, added_at DESC",
  :limit => params[:items_per_page],
  :offset => params[:offset]
  )

If you use the above plugin, you can do

return Article.find(
  :all,
  :order => :random,
  :limit => params[:items_per_page],
  :offset => 0
  )

To get (eg) 15 random items from the database. But, using this system,
every page will be a new random selection, so you might get the same
items on page 1, page 2, etc: in fact, the concept of ‘pages’ becomes
meaningless, you might as well just hit refresh to get another random
selection.

Alternatively, you can get all the records out of the db and randomize
the resulting array with

randomized_records = MyClass.find(:all).sort_by{rand}

Then, you can just ask for records (eg) 60 to 74 from this array, with

first_item = page_num*page_size
last_item = first_item + page_size - 1

page_of_records = randomized_records.slice[first_item…last_item]

This approach might be horrible for memory though - i don’t know how
many records you’re planning on holding but if its millions then this is
probably a bad idea.

As a third approach, you could make an array of indexes, randomize it,
then pull items out of the DB individually to make your pages:

indexes = []

MyClass.times do |i|
indexes << i
end

randomized_indexes = indexes.sort_by{rand}

Then, get items from the DB one by one:

page = []
first_item = page_num*page_size
last_item = first_item + page_size - 1
randomized_indexes.slice[first_item…last_item].each do |i|
page << MyClass.find_by_index(i)
end

Having said all of this, there’s probably a neat Rails helper to do all
of this :slight_smile:

How about page = rand(max_number_of_pages)?

– Long
http://MeandmyCity.com/ - Find your way
http://edgesoft.ca/blog/read/2 - No-Cookie Session Support plugin

----- Original Message -----
From: “Bala” [email protected]
To: “Ruby on Rails: Talk” [email protected]
Sent: Wednesday, October 03, 2007 2:22 AM
Subject: [Rails] Random Pagination

Thanks for the reply Expert,

but my problem is very different approach to random pagination.

likewise, i’ve home page where i’ve to display some categories
randomly with limit of 5 to 6 datas in a page,

if oracle doesnt support random function then how we solve this
issues? without complications.

regards,

Bala

Totally blocked :frowning:

On Oct 4, 5:30 pm, Frederick C. [email protected]

On 4 Oct 2007, at 11:30, Bala wrote:

I vaguely recall reading how wikipedia do their ‘random article thing’

  • each article is assigned a number between 0 and 1 (randomly and
    uniformly).
  • to pick a random article generate a random number p and pick the
    first article whose assigned number is greater than p

Fred

Really you should do the leg work yourself if you want it done. But here
is the pseudo as you seem
to need it.

  1. count the total number of rows - SQL
  2. calculate the max_number_of_pages - Ruby
  3. page = rand(max_number_of_pages) - Ruby
  4. calculate the offset - Ruby
  5. select the rows using ‘limit offset, items_per_page’ - SQL

If that doesn’t help, I’d have to agree with Frederick. ‘Say what?’

– Long

----- Original Message -----
From: “Bala” [email protected]
To: “Ruby on Rails: Talk” [email protected]
Sent: Thursday, October 04, 2007 5:30 AM
Subject: [Rails] Re: Random Pagination

regards,

Bala

On Oct 3, 6:57 pm, “Long” [email protected] wrote:

How about page = rand(max_number_of_pages)?

– Longhttp://MeandmyCity.com/- Find your wayhttp://edgesoft.ca/blog/read/2- No-Cookie Session
Support plugin

Bala wrote:

Hi Experts,

Is there way to do Random pagination?

I’m not with order, or sort basis. just random pagination

regards,

Bala

I’m quite into the idea of pseudo-random stuff.
So you could define some transformation on id
with a given set of parameters,

chuck those parameters into a session,
and recreate it.

although I havent bothered to think of a function.

A silly way could be;

session[:random_seed] ||= rand(50)
session[:random_direction] ||= [“ASC”, “DESC”][rand(2)]
Content.find(:all,
:order => “SUBSTRING(content.body, #{session[:random_seed]})
#{session[:random_direction]}”
)

But that won’t be fast, at all.

On 4 Oct 2007, at 14:37, Bala wrote:

Totally blocked :frowning:

Say what?

Fred

thank you guys, it works well,

@categories = Category.find(:all, :conditions =>

[“parentid=0”], :limit => 5, :offset => rand(5))

remember rand(5) is hardcoded

thanks for all your suggestions and help

regards,
Bala

On Oct 4, 10:25 pm, Matthew R. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs