<p>You need to create a common reference to the random number you are creating. Otherwise, you are getting a random number twice - and it's just luck that they show up as expected.</p>

<p>Try this:</p>

<pre><code>{# Get all of the matrix blocks for the global set #}
{% set globalSet = craft.globals.getSetByHandle('madLibs') %}

{% if globalSet|length %}

    {# Find out how many matrix blocks exist #}
    {% set madLibsCount = globalSet.madLib|length %} // 2

    {# Generate a random array key. Remember array is zero based #}
    {% set range = range(0, (madLibsCount - 1)) %}

    {# Capture a single (random) number #}
    {% set key = random(range) %}

    &lt;h1&gt;{{ globalSet.madLib[key].actualRole }}&lt;/h1&gt;
    &lt;p&gt;{{ globalSet.madLib[key].pretendRole }}&lt;/p&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2018-06-22T22:11:36.060Z
2018-06-22T22:11:36.060Z
Damon
<p>I am creating a mad libs type of global. My global set is actually called <code>madLibs</code>.</p>

<p>Within that set, is a matrix field - that can have unlimited blocks. Each block only has two fields. <code>actualRole</code> and <code>pretendRole</code>.</p>

<p>What I have is working - but sometimes I might get the <code>actualRole</code> from one matrix, and the <code>pretendRole</code> from another. Ideally, I'd like to get the same fields from the same random matrix.</p>

<pre><code>{# Get all of the matrix blocks for the global set #}
{% set globalSet = craft.globals.getSetByHandle('madLibs') %}

{% if globalSet|length %}

    {# Find out how many matrix blocks exist #}
    {% set madLibsCount = globalSet.madLib|length %} // 2

    {# Generate a random array key. Remember array is zero based #}
    {% set range = range(0, (madLibsCount - 1)) %}

    &lt;h1&gt;{{ globalSet.madLib[random(range)].actualRole }}&lt;/h1&gt;
    &lt;p&gt;{{ globalSet.madLib[random(range)].pretendRole }}&lt;/p&gt;
{% endif %}
</code></pre>

<p>Sometimes, this is happening:</p>

<pre><code>&lt;h1&gt;{{ globalSet.madLib[0].actualRole }}&lt;/h1&gt;
&lt;p&gt;{{ globalSet.madLib[1].pretendRole }}&lt;/p&gt;
</code></pre>

<p>I would really like to have this each time:</p>

<pre><code>&lt;h1&gt;{{ globalSet.madLib[0].actualRole }}&lt;/h1&gt;
&lt;p&gt;{{ globalSet.madLib[0].pretendRole }}&lt;/p&gt;
</code></pre>

<p>Thank you for any suggestions!</p>

<templating>
How can I get the same random array key?
# How can I get the same random array key?

(Asked by: username_0 on 2018-06-22)

I am creating a mad libs type of global. My global set is actually called madLibs.
Within that set, is a matrix field - that can have unlimited blocks. Each block only has two fields. actualRole and pretendRole.
What I have is working - but sometimes I might get the actualRole from one matrix, and the pretendRole from another. Ideally, I'd like to get the same fields from the same random matrix.
{# Get all of the matrix blocks for the global set #}
{% set globalSet = craft.globals.getSetByHandle('madLibs') %}

{% if globalSet|length %}

    {# Find out how many matrix blocks exist #}
    {% set madLibsCount = globalSet.madLib|length %} // 2

    {# Generate a random array key. Remember array is zero based #}
    {% set range = range(0, (madLibsCount - 1)) %}

    {{ globalSet.madLib[random(range)].actualRole }}
    {{ globalSet.madLib[random(range)].pretendRole }}
{% endif %}

Sometimes, this is happening:
{{ globalSet.madLib[0].actualRole }}
{{ globalSet.madLib[1].pretendRole }}

I would really like to have this each time:
{{ globalSet.madLib[0].actualRole }}
{{ globalSet.madLib[0].pretendRole }}

Thank you for any suggestions!

**Tags:** templating


---

## 1 answers

### Answer by username_0 on 2018-06-22. Score: 2 | Accepted answer

You need to create a common reference to the random number you are creating. Otherwise, you are getting a random number twice - and it's just luck that they show up as expected.
Try this:
{# Get all of the matrix blocks for the global set #}
{% set globalSet = craft.globals.getSetByHandle('madLibs') %}

{% if globalSet|length %}

    {# Find out how many matrix blocks exist #}
    {% set madLibsCount = globalSet.madLib|length %} // 2

    {# Generate a random array key. Remember array is zero based #}
    {% set range = range(0, (madLibsCount - 1)) %}

    {# Capture a single (random) number #}
    {% set key = random(range) %}

    {{ globalSet.madLib[key].actualRole }}
    {{ globalSet.madLib[key].pretendRole }}
{% endif %}


---

