# Choose 6 records from the database randomly

**URL:** <https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056>\
**Category:** Rails\
**Created:** [December 4, 2007, 7:43am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056 "2007-12-04T07:43:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 7:43am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/1 "2007-12-04T07:43:19Z")

</div>

Hello All,

I have to choose 6 records from the table but randomly.  
I have written the code:  
@products = Product.find(:all, :conditions =\> “feature = 1”, :limit =\>  
6)  
But it fetches only the first six records from the table. I want to  
choose only 6 products but in random order not the first six.

Please tell me how to implement it.  
Any help would be greatly appreciated.

Thanks,  
Rohit.

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 7:59am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/2 "2007-12-04T07:59:08Z")

</div>

Get all the product ids:  
ids = Product.find(:all, :select =\> :id).map(&:id)

Choose six at random"  
random\_ids = … # left as an exercise for the reader

@products = Product.find(random\_ids)

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 9:59am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/3 "2007-12-04T09:59:48Z")

</div>

On Dec 4, 6:58 am, kevin cline [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Get all the product ids:  
> ids = Product.find(:all, :select =\> :id).map(&:id)
> 
> Choose six at random"  
> random\_ids = … # left as an exercise for the reader

That will be incredibly inefficient. ‘Select \* from products order by  
rand() limit 6’ is also not massively fast.

One fast way of doing this (which happens to be the way wikipedia does  
their random article search):  
assign to every product a random number between 0 and 1

When you want to get some random products, then pick a random number  
and select the products whose random value is great than the number  
you’ve just picked. If you’ve got an index on the column, this should  
be pretty speedy.

Fred

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 10:18am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/4 "2007-12-04T10:18:21Z")

</div>

Hi Fred,

Sorry…am not able to understand what you have written.  
How to assign a random number between 0 to 1 to a product?  
Could you please explain?

Thanks,  
Rohit.

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 10:27am UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/5 "2007-12-04T10:27:22Z")

</div>

On Dec 4, 2007 9:18 AM, Rohit M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Fred,
> 
> Sorry…am not able to understand what you have written.  
> How to assign a random number between 0 to 1 to a product?  
> Could you please explain?

Well you have a column on the products table that contains a random  
number.

Fred

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/6 "2007-12-04T12:37:35Z")

</div>

You could use custom SQL SELECT … FROM my\_table ORDER BY RAND()  
LIMIT (N)

On Dec 4, 6:43 am, Rohit M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/7 "2007-12-04T12:44:38Z")

</div>

He Fred, that’s a slick idea. I was mulling over the best way to do  
this – in the course of programming an online game, I needed to pick  
n contestants from a pool of arbitrary size, then narrow that to n/2  
contestants, then n/4, finally ending up with just one.

Adding and indexing a random number beforehand sounds like a good way  
to go. Thanks for the thought!

---

<div class="post-metadata">

**Author:** ![ror-user](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ror-user](https://www.ruby-forum.com/u/ror-user)\
**Post date:** [December 4, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/choose-6-records-from-the-database-randomly/122056/8 "2007-12-04T16:38:53Z")

</div>

Really cool …

On Dec 4, 4:59 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
