<p>There are two options for accomplishing this:</p>

<pre><code>{% for block in shuffle(entry.matrixField) %}
</code></pre>

<p>or </p>

<pre><code>{% for block in shuffle(entry.matrixField.find()) %}﻿
</code></pre>

CC BY-SA 3.0
2014-06-12T19:34:15.240Z
I think these are very much the same. `entry.matrixField.find()` explicitly returns an array, but `shuffle()` will convert to an array anyway
Marion Newlevant
CC BY-SA 3.0
2014-06-12T17:24:41.603Z
2014-06-12T17:24:41.603Z
Anna_MediaGirl
<p>You can either have MySQL select the blocks in a random order using <a href="http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html#function_rand"><code>RAND()</code></a>:</p>

<pre><code>{% set blocks = entry.myMatrixField.order('RAND()') %}
</code></pre>

<p>Or you can shuffle the blocks from Twig instead, using Craft’s <a href="http://buildwithcraft.com/docs/templating/functions#shuffle">shuffle()</a> function:</p>

<pre><code>{% set blocks = shuffle(entry.myMatrixField) %}
</code></pre>

<p>There’s not a major performance difference either way in most cases, however if you also want to <em>limit</em> the number of blocks, each method will give you different results.</p>

<p>For example, this will pick 3 blocks at random out of the entire set:</p>

<pre><code>{% set blocks = entry.myMatrixField.limit(3).order('RAND()') %}
</code></pre>

<p>But this will always pick the first 3 blocks, and those 3 blocks will be displayed in a random order:</p>

<pre><code>{% set blocks = shuffle(entry.myMatrixField.limit(3)) %}
</code></pre>

CC BY-SA 3.0
2014-06-12T17:59:06.537Z
2014-06-12T17:59:06.537Z
Brandon Kelly
<p>I have a slideshow with slide data managed in a Matrix field. How would I go about randomizing the data returned in the template so the slides show in a different order on each page load?</p>

<matrix><random><ordering>
How can I return data from a Matrix field in random order?
# How can I return data from a Matrix field in random order?

(Asked by: username_0 on 2014-06-12)

I have a slideshow with slide data managed in a Matrix field. How would I go about randomizing the data returned in the template so the slides show in a different order on each page load?

**Tags:** matrix, random, ordering

---

## 2 answers

### Answer by username_2 on 2014-06-12. Score: 27 | Accepted answer

You can either have MySQL select the blocks in a random order using RAND():
{% set blocks = entry.myMatrixField.order('RAND()') %}

Or you can shuffle the blocks from Twig instead, using Craft’s shuffle() function:
{% set blocks = shuffle(entry.myMatrixField) %}

There’s not a major performance difference either way in most cases, however if you also want to limit the number of blocks, each method will give you different results.
For example, this will pick 3 blocks at random out of the entire set:
{% set blocks = entry.myMatrixField.limit(3).order('RAND()') %}

But this will always pick the first 3 blocks, and those 3 blocks will be displayed in a random order:
{% set blocks = shuffle(entry.myMatrixField.limit(3)) %}


---

### Answer by username_0 on 2014-06-12. Score: 2

There are two options for accomplishing this:
{% for block in shuffle(entry.matrixField) %}

or 
{% for block in shuffle(entry.matrixField.find()) %}﻿

> **Comments:**
> **username_1**: I think these are very much the same. `entry.matrixField.find()` explicitly returns an array, but `shuffle()` will convert to an array anyway  

---

