# Rand() order and Pagination

**URL:** <https://www.ruby-forum.com/t/rand-order-and-pagination/49099>\
**Category:** Rails\
**Created:** [November 29, 2005, 10:26pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099 "2005-11-29T22:26:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [November 29, 2005, 10:26pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/1 "2005-11-29T22:26:03Z")

</div>

I’m stumped with how to combine pagination and a rand order\_by from my  
model.

Let’s say I am a teacher and have a table full of test questions. I want  
to  
return a query of 20 questions and use the paginator to return 1 or 5 or  
10  
per page. The catch is I want the 20 question to be random and in random  
order so they don’t keep getting the first 20 questions.

On each paginated page, the paginator performs a new query using a limit  
X  
and an offset X to return different sequential results. If I use  
order\_by  
rand() in my query then I will get a random result, but there is a  
chance I  
will get a duplicate question.

Does anyone have any suggestions on how to return random results with  
pagination?

Thanks,

Steve

---

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [November 29, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/2 "2005-11-29T23:34:44Z")

</div>

One way to tackle this would be to select randomly into a temporary  
table,  
then paginate that table.

–another steve

---

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [November 29, 2005, 11:33pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/3 "2005-11-29T23:33:10Z")

</div>

steve.odom wrote:

> I’m stumped with how to combine pagination and a rand order\_by from my  
> model.
> 
> Let’s say I am a teacher and have a table full of test questions. I want  
> to  
> return a query of 20 questions and use the paginator to return 1 or 5 or  
> 10  
> per page. The catch is I want the 20 question to be random and in random  
> order so they don’t keep getting the first 20 questions.
> 
> On each paginated page, the paginator performs a new query using a limit  
> X  
> and an offset X to return different sequential results. If I use  
> order\_by  
> rand() in my query then I will get a random result, but there is a  
> chance I  
> will get a duplicate question.
> 
> Does anyone have any suggestions on how to return random results with  
> pagination?
> 
> Thanks,
> 
> Steve

This should do it:

## in application.rb:

def paginate\_collection(collection, options = {})  
default\_options = {:per\_page =\> 10, :page =\> 1}  
options = default\_options.merge options

```
 pages = Paginator.new self, collection.size, options[:per_page], 

```

options[:page]  
first = pages.current.offset  
last = [first + options[:per\_page], collection.size].min  
slice = collection[first…last]  
return [pages, slice]  
end

## in your controller:

@question\_pages, @questions = paginate\_collection Question.find(:all,  
:order =\> “rand()”), :page =\> @params[:page]

–Jonathan

---

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [December 1, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/4 "2005-12-01T15:56:51Z")

</div>

This worked great. Thanks!

---

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [November 30, 2005, 1:07am UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/5 "2005-11-30T01:07:37Z")

</div>

Thanks to both of you for the suggestions. I’m going to have to work  
through  
Jonathan’s suggestion - I don’t quite grasp it at first glance. I’m  
using a  
temp table solution to do something else, so that approach is a  
possiblity.

Steve

---

<div class="post-metadata">

**Author:** ![steve.odom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@steve.odom](https://www.ruby-forum.com/u/steve.odom)\
**Post date:** [December 1, 2005, 4:49pm UTC](https://www.ruby-forum.com/t/rand-order-and-pagination/49099/6 "2005-12-01T16:49:57Z")

</div>

Jonathan,

Wouldn’t this give you a new randomized list on every page? ie. when  
you’re  
on page one, you get a the first set of 10 random questions, then on  
page 2  
rand() is called again and you get another 10 random numbers which may  
include questions that were displayed on the first page.

Am I missing something?

Thanks

Hammed
