Answers.Body: <p>You need some way to keep track of which entries have been shown to the current user already, then exclude those entries on subsequent requests. One way to do this is using the session storage. However, this means that the list of entries already displayed will only persist for the current browser session. If you only need to provide this functionality for logged-in users, you can instead associate the list of entries with their account, thereby persisting it across sessions and devices.</p>
  <p>This means you will need some kind of persistent storage. You could either write the list to a file or a custom database table, but for simplicity, I would just the <a href="https://www.yiiframework.com/doc/guide/2.0/en/caching-data" rel="nofollow noreferrer">cache storage</a>.</p>
  <pre class="lang-twig prettyprint-override"><code>{# 1 #}
  {% set cache = craft.app.cache %}
  {% set cacheKey = 'entries-already-shown-' ~ (currentUser.id ?? 0) %}
  {% set alreadySeenIds = cache.get(cacheKey)|default([]) %}
  
  {# 2 #}
  {% set limit = 5 %}
  {% set newRandomEntriesQuery = craft.entries()
      .section('my_section')
      .orderBy('RAND()')
      .limit(limit)
      .all()
  %}
  {% if alreadySeenIds %}
      {% do newRandomEntriesQuery.id(['not']|merge(alreadySeenIds)) %}
  {% endif %}
  {% set newRandomEntries = newRandomEntriesQuery.all() %}
  
  {# 3 #}
  {% set alreadySeenIds = alreadySeenIds|merge(newRandomEntries|column('id')) %}
  {% do cache.set(cacheKey, alreadySeenIds, 31536000) %}
  
  {# 4 #}
  {% if newRandomEntries|length &lt; limit %}
      {% do cache.delete(cacheKey) %}
  {% endif %}
  </code></pre>
  <p>Let's look at that in more detail:</p>
  <ol>
  <li>We use the <a href="https://www.yiiframework.com/doc/guide/2.0/en/caching-data" rel="nofollow noreferrer">Cache component</a> to get a list of entry IDs that were already shown to the current user. The cache key includes the current user's ID, so the cache will be unique to that user. The default is an empty array, if the user hasn't seen any entries yet.</li>
  <li>We get a list of new randomly selected entries (replace <code>my_section</code> with the section that the entries you want to show live in, and add any additional required query parameters). We exclude all the entries this user has already seen by their IDs.</li>
  <li>We update the list of entry IDs the user has already seen, merging the new ones with the existing list. The cache duration <code>31536000</code> is one year in seconds, but you can also choose a smaller value if you want to reset the list when a user hasn't viewed the site in a while.</li>
  <li>If there are fewer entries than the allowed limit, that means the user has seen all the available entries already. In this case, we reset the list of already seen entries.</li>
  </ol>
  <p>There are some improvements that could be made to the code above. For example, when the user has seen almost all entries, the last request may show fewer (or even zero) entries. You could update step 4 to backfill the list from the full set of entries and start a new list of entries already seen in this case. To make handling those edge-cases easier, I suggest moving all that logic out of Twig and into PHP using a <a href="https://craftcms.com/docs/4.x/extend/module-guide.html" rel="nofollow noreferrer">custom module</a>. The module can <a href="https://craftcms.com/docs/4.x/extend/extending-twig.html" rel="nofollow noreferrer">expose a Twig function</a> like <code>getRandomEntriesForUser()</code> that retrieves the next set of random entries using this logic. This also makes it easy to reuse that logic in different templates.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-03T01:00:57.600Z
Answers.Text: This is clever and detailed.
  Thank you very much for the accuracy and explanation in the answer.
  I appreciate your effort and time in writing this response. I will try this solution.
  Thanks again, MoritzLost.
Answers.UserDisplayName: Mohamed
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-02T13:52:11.887Z
Answers.LastActivityDate: 2023-04-02T13:58:58.233Z
Answers.LastEditDate: 2023-04-02T13:58:58.233Z
Answers.UserDisplayName: MoritzLost
Body: <p>I want to show entries (hundreds) for signed-in users randomly, I use this:</p>
  <pre><code>.order('RAND()')
  </code></pre>
  <p>and in the same time make sure to <strong>loop over all entries before start showing the entries that have been shown</strong>.
  Can you please help me?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-01T23:24:16.303Z
Comments.Text: Why do you want to loop over them before displaying them?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-03T00:56:50.633Z
Comments.Text: My expression may not be clear. I want to display the entries. However, the point is that I want to make sure it doesn't re-display them (since it is randomly) before going through all the rest of them (entries that have been not displayed).
Comments.UserDisplayName: Mohamed
Tags: <templating><random>
Title: How to show entries randomly
ThreadText: # How to show entries randomly
  
  (Asked by: username_0 on 2023-04-01)
  
  I want to show entries (hundreds) for signed-in users randomly, I use this:
  
  ```
  .order('RAND()')
  
  ```
  
  and in the same time make sure to **loop over all entries before start showing the entries that have been shown**.
  Can you please help me?
  
  **Tags:** templating, random
  
  > **Comments:**
  > **username_1**: Why do you want to loop over them before displaying them?  
  > **username_0**: My expression may not be clear. I want to display the entries. However, the point is that I want to make sure it doesn't re-display them (since it is randomly) before going through all the rest of them (entries that have been not displayed).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-04-02. Score: 3
  
  You need some way to keep track of which entries have been shown to the current user already, then exclude those entries on subsequent requests. One way to do this is using the session storage. However, this means that the list of entries already displayed will only persist for the current browser session. If you only need to provide this functionality for logged-in users, you can instead associate the list of entries with their account, thereby persisting it across sessions and devices.
  
  This means you will need some kind of persistent storage. You could either write the list to a file or a custom database table, but for simplicity, I would just the [cache storage](https://www.yiiframework.com/doc/guide/2.0/en/caching-data).
  
  ```twig
  {# 1 #}
  {% set cache = craft.app.cache %}
  {% set cacheKey = 'entries-already-shown-' ~ (currentUser.id ?? 0) %}
  {% set alreadySeenIds = cache.get(cacheKey)|default([]) %}
  
  {# 2 #}
  {% set limit = 5 %}
  {% set newRandomEntriesQuery = craft.entries()
      .section('my_section')
      .orderBy('RAND()')
      .limit(limit)
      .all()
  %}
  {% if alreadySeenIds %}
      {% do newRandomEntriesQuery.id(['not']|merge(alreadySeenIds)) %}
  {% endif %}
  {% set newRandomEntries = newRandomEntriesQuery.all() %}
  
  {# 3 #}
  {% set alreadySeenIds = alreadySeenIds|merge(newRandomEntries|column('id')) %}
  {% do cache.set(cacheKey, alreadySeenIds, 31536000) %}
  
  {# 4 #}
  {% if newRandomEntries|length < limit %}
      {% do cache.delete(cacheKey) %}
  {% endif %}
  
  ```
  
  Let's look at that in more detail:
  
  1. We use the [Cache component](https://www.yiiframework.com/doc/guide/2.0/en/caching-data) to get a list of entry IDs that were already shown to the current user. The cache key includes the current user's ID, so the cache will be unique to that user. The default is an empty array, if the user hasn't seen any entries yet.
  2. We get a list of new randomly selected entries (replace `my_section` with the section that the entries you want to show live in, and add any additional required query parameters). We exclude all the entries this user has already seen by their IDs.
  3. We update the list of entry IDs the user has already seen, merging the new ones with the existing list. The cache duration `31536000` is one year in seconds, but you can also choose a smaller value if you want to reset the list when a user hasn't viewed the site in a while.
  4. If there are fewer entries than the allowed limit, that means the user has seen all the available entries already. In this case, we reset the list of already seen entries.
  
  There are some improvements that could be made to the code above. For example, when the user has seen almost all entries, the last request may show fewer (or even zero) entries. You could update step 4 to backfill the list from the full set of entries and start a new list of entries already seen in this case. To make handling those edge-cases easier, I suggest moving all that logic out of Twig and into PHP using a [custom module](https://craftcms.com/docs/4.x/extend/module-guide.html). The module can [expose a Twig function](https://craftcms.com/docs/4.x/extend/extending-twig.html) like `getRandomEntriesForUser()` that retrieves the next set of random entries using this logic. This also makes it easy to reuse that logic in different templates.
  
  > **Comments:**
  > **username_0**: This is clever and detailed. Thank you very much for the accuracy and explanation in the answer. I appreciate your effort and time in writing this response. I will try this solution. Thanks again, username_2.  
  
  ---
  
  