<p>You're always getting the same set because the entries are first fetched, then shuffled. To achieve what you want, you could do:</p>

<pre class="lang-twig prettyprint-override"><code>{# get all your entries ids #}
{% set allIds = craft.entries().section('yourSection').ids() %}
{# Shuffle all and slice to just 15 #}
{% set shuffledIds = shuffle(allIds) | slice(0,15) %}
{# Fetch your random entries #}
{% set entries = craft.entries.id(shuffledIds).all() %}
</code></pre>

<p>Or</p>

<pre class="lang-twig prettyprint-override"><code>{# get all your entries ids #}
{% set allIds = craft.entries().section('yourSection').ids() %}
{# Fetch your random entries #}
{% set entries = craft.entries().id(shuffle(allIds) | slice(0,15)).all() %}
</code></pre>

<p>In response to your comment then you could do something like the below:</p>

<pre class="lang-twig prettyprint-override"><code>{# set a random offset #}
{% set offset = random(0, 6000)%}

{# set a random sort #}
{% set orderBy = random(['postDate', 'id', 'title']) %}

{# set a random direction #}
{% set direction = random(['ASC', 'DESC']) %}

{# get 500 ids #}
{% set allIds = craft
            .entries()
            .section('posts')
            .offset(offset)
            .orderBy(orderBy ~ ' ' ~ direction)
            .limit(500)
            .ids() %}

{# Fetch your random entries #}
{% set entries = craft.entries().id(shuffle(allIds) | slice(0,15)).all() %}
</code></pre>

<p>With this, you only fetch 500 entries with a random offset and sorting, shuffle that pool and pick 15. This might not be <em>as</em> random but I doubt you'd notice it.</p>

CC BY-SA 4.0
2020-03-05T14:01:16.093Z
Due to it being a fairly large site with over 8k entries. Would this cause strain to fetch this many ID's?
JMKelley
CC BY-SA 4.0
2020-03-05T14:25:39.697Z
@JMKelley just added another option for you
Oli
CC BY-SA 4.0
2020-03-05T13:59:10.997Z
2020-03-05T14:32:55.407Z
2020-03-05T14:32:55.407Z
Oli
<p>You can use the orberBy('RAND()') function. I learned this one just the other day. (It might only be for Craft CMS 4)</p>
<pre><code>{% set nextEntries = craft.entries.section('merchants').id('not ' ~ entry.id).limit(15).orderBy('RAND()') %}
</code></pre>

CC BY-SA 4.0
2022-10-05T23:17:21.017Z
2022-10-05T23:17:21.017Z
mateostabio
<p>I'm trying to create a "Hack Job" version of Similar Entries, I'm using the following code but it seems to be getting the same entries over and over again. I know shuffle function does this, but I thought it'd be shuffling through the entire array of Entries?</p>

<pre><code>{% set nextEntries = shuffle(craft.entries.section('merchants').id('not ' ~ entry.id).limit(15)) %}
</code></pre>

<p>I'm looking to make it grab a random 15 entries from the <code>merchants</code> section, which I will then cache.</p>

<p>Any help would be great.</p>

<templating>
Get Random Set of Entries
# Get Random Set of Entries

(Asked by: username_0 on 2020-03-05)

I'm trying to create a "Hack Job" version of Similar Entries, I'm using the following code but it seems to be getting the same entries over and over again. I know shuffle function does this, but I thought it'd be shuffling through the entire array of Entries?
{% set nextEntries = shuffle(craft.entries.section('merchants').id('not ' ~ entry.id).limit(15)) %}

I'm looking to make it grab a random 15 entries from the merchants section, which I will then cache.
Any help would be great.

**Tags:** templating


---

## 2 answers

### Answer by username_1 on 2020-03-05. Score: 2 | Accepted answer

You're always getting the same set because the entries are first fetched, then shuffled. To achieve what you want, you could do:
{# get all your entries ids #}
{% set allIds = craft.entries().section('yourSection').ids() %}
{# Shuffle all and slice to just 15 #}
{% set shuffledIds = shuffle(allIds) | slice(0,15) %}
{# Fetch your random entries #}
{% set entries = craft.entries.id(shuffledIds).all() %}

Or
{# get all your entries ids #}
{% set allIds = craft.entries().section('yourSection').ids() %}
{# Fetch your random entries #}
{% set entries = craft.entries().id(shuffle(allIds) | slice(0,15)).all() %}

In response to your comment then you could do something like the below:
{# set a random offset #}
{% set offset = random(0, 6000)%}

{# set a random sort #}
{% set orderBy = random(['postDate', 'id', 'title']) %}

{# set a random direction #}
{% set direction = random(['ASC', 'DESC']) %}

{# get 500 ids #}
{% set allIds = craft
            .entries()
            .section('posts')
            .offset(offset)
            .orderBy(orderBy ~ ' ' ~ direction)
            .limit(500)
            .ids() %}

{# Fetch your random entries #}
{% set entries = craft.entries().id(shuffle(allIds) | slice(0,15)).all() %}

With this, you only fetch 500 entries with a random offset and sorting, shuffle that pool and pick 15. This might not be as random but I doubt you'd notice it.

> **Comments:**
> **username_0**: Due to it being a fairly large site with over 8k entries. Would this cause strain to fetch this many ID's?  
> **username_1**: @username_0 just added another option for you  

---

### Answer by username_2 on 2022-10-05. Score: 0

You can use the orberBy('RAND()') function. I learned this one just the other day. (It might only be for Craft CMS 4)
{% set nextEntries = craft.entries.section('merchants').id('not ' ~ entry.id).limit(15).orderBy('RAND()') %}


---

