---
title: "Displaying non repeating random records"
description:
  "Displaying random records is easy. But sometimes it can show duplicate
  records. This blog discusses how to show random records without showing
  duplicate records."
canonical_url: "https://www.bigbinary.com/blog/non-repeating-random-records"
markdown_url: "https://www.bigbinary.com/blog/non-repeating-random-records.md"
---

# Displaying non repeating random records

Displaying random records is easy. But sometimes it can show duplicate records.
This blog discusses how to show random records without showing duplicate
records.

- Author: Neeraj Singh
- Published: November 13, 2013
- Categories: Rails

For one of our clients we need to display random records from the database.
That's easy enough. We can use `random()` function.

```ruby
Batch.published_and_featured.order('random()')
                            .paginate(per_page: 20, page: params[:page])
```

Here we are using PostgreSQL database but ,I believe, above query will also work
on MySQL.

The problem here is that if the user clicks on next page then we will try to get
next set of 20 random records. And since these records are truly random,
sometimes the user might see the records which has already been seen in the
first page.

The fix is to make it random but not truly random. It needs to be random with a
seed.

## Fix in MySQL

In MySQL we can pass seed directly to `random()` function.

```ruby
Batch.published_and_featured.order('random(0.3)')
                            .paginate(per_page: 20, page: params[:page])
```

## Fix in PostgreSQL

In PostgreSQL it is a little more cumbersome. We first need to set seed and then
the subsequent query's usage of `random()` will make use of seed value.

```ruby
Batch.connection.execute "SELECT setseed(0.2)"
Batch.published_and_featured.order('random()')
                            .paginate(per_page: 20, page: params[:page])
```

## Set seed value in before_action

For different user we should use different seed value and this value should be
random. So we set the seed value in `before_action`.

```ruby
def set_seed
  cookies[:random_seed] ||= SecureRandom.random_number
end
```

Now change the query to use the seed value and we are all set.

## Links

- [Human page](https://www.bigbinary.com/blog/non-repeating-random-records)
