Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-22T22:11:36.060Z
Answers.LastActivityDate: 2018-06-22T22:11:36.060Z
Answers.UserDisplayName: Damon
Body: <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>
  
Tags: <templating>
Title: How can I get the same random array key?
ThreadText: # 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)) %}
  
      <h1>{{ globalSet.madLib[random(range)].actualRole }}</h1>
      <p>{{ globalSet.madLib[random(range)].pretendRole }}</p>
  {% endif %}
  
  ```
  
  Sometimes, this is happening:
  
  ```
  <h1>{{ globalSet.madLib[0].actualRole }}</h1>
  <p>{{ globalSet.madLib[1].pretendRole }}</p>
  
  ```
  
  I would really like to have this each time:
  
  ```
  <h1>{{ globalSet.madLib[0].actualRole }}</h1>
  <p>{{ globalSet.madLib[0].pretendRole }}</p>
  
  ```
  
  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) %}
  
      <h1>{{ globalSet.madLib[key].actualRole }}</h1>
      <p>{{ globalSet.madLib[key].pretendRole }}</p>
  {% endif %}
  
  ```
  
  
  ---
  
  