Answers.Body: <p>No need to loop. You can use <code>random()</code>:</p>
  
  <pre><code>{% set randomEntry = random(entry.entriesFieldHandle) %}
  {% if randomEntry %}
      {% set randomUrl = randomEntry.url %}
  {% endif %}
  </code></pre>
  
  <p><code>randomUrl</code> is your random entry's url you wanted to get.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:40:53.117Z
Answers.Text: [random](http://twig.sensiolabs.org/doc/functions/random.html) is a twig function. `random(entry.entriesFieldHandle)` will retrieve all the entries, then pick a random one.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:43:14.007Z
Answers.Text: @Marion Is anything bad in it? OP seems to ask about twig...  Sorry, not really so experienced :-)
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:47:52.430Z
Answers.Text: Nothing wrong with it. Just responding to @ChristianSeelbach comment on my answer. Picking the random entry with the query is probably more efficient if there are a lot of entries, but your code is simpler. Should check that it found something though.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:50:02.797Z
Answers.Text: @Marion You mean if it found an entry? Well, why not...
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:51:00.153Z
Answers.Text: @micael yes. If there are no entries, randomEntry.url will give an error
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T14:51:40.920Z
Answers.Text: @Marion Now? :-) And I seem to be nicael, not micael :D And no need to add `@reply` if you respond to the post author - he is automatically notified.
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-11T23:27:25.310Z
Answers.LastActivityDate: 2014-07-12T14:50:54.587Z
Answers.LastEditDate: 2014-07-12T14:50:54.587Z
Answers.UserDisplayName: nicael
Answers.Body: <p>It is possible to have the query pick a random entry.
  From the Craft <a href="http://buildwithcraft.com/docs/entries-fields#templating" rel="nofollow">Entries Fields</a> docs we see that <code>entry.entriesFieldHandle</code> is a shortcut for</p>
  
  <pre><code>craft.entries({
    relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
    order: "sortOrder",
    limit: null
  })
  </code></pre>
  
  <p>And what we want is to order the entries randomly, and then limit the number we retrieve to only one:</p>
  
  <pre><code>craft.entries({
    relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
    order: "RAND()",
    limit: 1
  })
  </code></pre>
  
  <p>So to retrieve one random entry and get its url:</p>
  
  <pre><code>{% set randomEntry = craft.entries({
        relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
        order: "RAND()",
        limit: 1
      }).first()
  %}
  {% if randomEntry %}
    {% set randomUrl = randomEntry.url %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T11:37:38.187Z
Answers.Text: Interesting answer, Marion! Performance is better with your second approach, right? Then I'd leave out the random() part or put it at the end of the answer.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-13T06:58:50.943Z
Answers.Text: _(Sorry for the repost, I found a big error in my old comment.)_ Very cool way to do it! Using your last example I keep getting randomEntry returning as null. All I've changed is my "field:" to "aGlobalSet.entriesField". Any ideas why it isn't working?
Answers.UserDisplayName: Simon Kuran
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-13T23:21:58.047Z
Answers.Text: @SimonKuran to pick a random entry from `aGlobalSet` you want `{sourceElement: aGlobalSet, field: "entriesField"}`
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-13T23:45:24.950Z
Answers.Text: Ah, I get it now. It worked, thanks! I put an edit in the queue adding the "all at once" technique I used to your answer.
Answers.UserDisplayName: Simon Kuran
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T00:04:49.717Z
Answers.Text: I think the more common use case is that someone wants a random entry (with title, url, etc.), not just the url of a random entry
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T00:38:57.060Z
Answers.Text: Oh, I'm sure it is. My example just better matched the question, but it's fairly self-explanatory to simplify down to it. It's totally fine added or not. :)
Answers.UserDisplayName: Simon Kuran
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-12T04:18:15.633Z
Answers.LastActivityDate: 2014-07-12T14:40:30.257Z
Answers.LastEditDate: 2014-07-12T14:40:30.257Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I have a Entries field and I'd like to return a URL for one randomly picked entry.</p>
  
  <p>I could loop through all the entries, but I'd like to do something more efficient. Can I return an array of URLs? Or better yet, is it possible to randomly get one URL all at once?</p>
  
Tags: <templating><entries><url><random>
Title: How do I get a random entry's URL from a Entries field?
ThreadText: # How do I get a random entry's URL from a Entries field?
  
  (Asked by: username_0 on 2014-07-11)
  
  I have a Entries field and I'd like to return a URL for one randomly picked entry.
  
  I could loop through all the entries, but I'd like to do something more efficient. Can I return an array of URLs? Or better yet, is it possible to randomly get one URL all at once?
  
  **Tags:** templating, entries, url, random
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-07-12. Score: 7 | Accepted answer
  
  It is possible to have the query pick a random entry.
  From the Craft [Entries Fields](http://buildwithcraft.com/docs/entries-fields#templating) docs we see that `entry.entriesFieldHandle` is a shortcut for
  
  ```
  craft.entries({
    relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
    order: "sortOrder",
    limit: null
  })
  
  ```
  
  And what we want is to order the entries randomly, and then limit the number we retrieve to only one:
  
  ```
  craft.entries({
    relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
    order: "RAND()",
    limit: 1
  })
  
  ```
  
  So to retrieve one random entry and get its url:
  
  ```
  {% set randomEntry = craft.entries({
        relatedTo: { sourceElement: entry, field: "entriesFieldHandle" },
        order: "RAND()",
        limit: 1
      }).first()
  %}
  {% if randomEntry %}
    {% set randomUrl = randomEntry.url %}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_3**: Interesting answer, username_2! Performance is better with your second approach, right? Then I'd leave out the random() part or put it at the end of the answer.  
  > **username_0**: _(Sorry for the repost, I found a big error in my old comment.)_ Very cool way to do it! Using your last example I keep getting randomEntry returning as null. All I've changed is my "field:" to "aGlobalSet.entriesField". Any ideas why it isn't working?  
  > **username_2**: @username_0 to pick a random entry from `aGlobalSet` you want `{sourceElement: aGlobalSet, field: "entriesField"}`  
  > **username_0**: Ah, I get it now. It worked, thanks! I put an edit in the queue adding the "all at once" technique I used to your answer.  
  > **username_2**: I think the more common use case is that someone wants a random entry (with title, url, etc.), not just the url of a random entry  
  > **username_0**: Oh, I'm sure it is. My example just better matched the question, but it's fairly self-explanatory to simplify down to it. It's totally fine added or not. :)  
  
  ---
  
  ### Answer by username_1 on 2014-07-11. Score: 3
  
  No need to loop. You can use `random()`:
  
  ```
  {% set randomEntry = random(entry.entriesFieldHandle) %}
  {% if randomEntry %}
      {% set randomUrl = randomEntry.url %}
  {% endif %}
  
  ```
  
  `randomUrl` is your random entry's url you wanted to get.
  
  > **Comments:**
  > **username_2**: [random](http://twig.sensiolabs.org/doc/functions/random.html) is a twig function. `random(entry.entriesFieldHandle)` will retrieve all the entries, then pick a random one.  
  > **username_1**: @username_2 Is anything bad in it? OP seems to ask about twig...  Sorry, not really so experienced :-)  
  > **username_2**: Nothing wrong with it. Just responding to @ChristianSeelbach comment on my answer. Picking the random entry with the query is probably more efficient if there are a lot of entries, but your code is simpler. Should check that it found something though.  
  > **username_1**: @username_2 You mean if it found an entry? Well, why not...  
  > **username_2**: @micael yes. If there are no entries, randomEntry.url will give an error  
  > **username_1**: @username_2 Now? :-) And I seem to be username_1, not micael :D And no need to add `@reply` if you respond to the post author - he is automatically notified.  
  
  ---
  
  