Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-05T14:01:16.093Z
Answers.Text: Due to it being a fairly large site with over 8k entries. Would this cause strain to fetch this many ID's?
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-05T14:25:39.697Z
Answers.Text: @JMKelley just added another option for you
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-05T13:59:10.997Z
Answers.LastActivityDate: 2020-03-05T14:32:55.407Z
Answers.LastEditDate: 2020-03-05T14:32:55.407Z
Answers.UserDisplayName: Oli
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-05T23:17:21.017Z
Answers.LastActivityDate: 2022-10-05T23:17:21.017Z
Answers.UserDisplayName: mateostabio
Body: <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>
  
Tags: <templating>
Title: Get Random Set of Entries
ThreadText: # 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:
  
  ```twig
  {# 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
  
  ```twig
  {# 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:
  
  ```twig
  {# 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()') %}
  
  ```
  
  
  ---
  
  