Answers.Body: <p>Instead of using <a href="http://twig.sensiolabs.org/doc/extensions/array.html" rel="nofollow">shuffle</a>, which is only available in the extra <a href="http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install" rel="nofollow">Array extensions library</a>, you could just use <a href="http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install" rel="nofollow">random</a>.</p>
  
  <pre><code>{% set range = range(10, 90) %}
  
  {% set randomHeight1 = random(range) %}
  {% set randomWidth1 = random(range) %}
  
  {% set randomHeight2 = random(range) %}
  {% set randomWidth2 = random(range) %}
  
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-06T15:07:29.720Z
Answers.Text: There is this crazy cool CMS named Craft, that adds some Twig functions on top of the default ones and [`shuffle`](https://craftcms.com/docs/templating/functions#shuffle) is one of them! ;-)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-06T15:11:52.103Z
Answers.Text: I'll have to check it out sometime! :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T17:21:34.033Z
Answers.LastActivityDate: 2016-08-05T17:21:34.033Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Building off of Brad's answer, you might consider using a <code>for</code> loop to render your markup, instead of setting all of those variables:</p>
  
  <pre><code>{% set range = range(10, 90) %}
  {% set totalDivs = 5 %}
  
  &lt;div class="sandbox"&gt;
    {% for i in 1..totalDivs %}
      &lt;div style="left: {{ random(range) }}%; top: {{ random(range) }}%;"&gt;&lt;/div&gt;
    {% endfor %}
  &lt;/div&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T20:06:05.590Z
Answers.LastActivityDate: 2016-08-05T20:06:05.590Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>What is the best way to get a random number within a certain range?</p>
  
  <p>I made something but I guess there is a better way:</p>
  
  <p>In this example I want to position some elements randomly. This approach works, but I have to define a variable for each value.</p>
  
  <pre><code>{% set randomizr = range(10, 90) %}
  
  {% set randomHeight1 = shuffle(randomizr) | first %}
  {% set randomWidth1 = shuffle(randomizr) | first %}
  
  {% set randomHeight2 = shuffle(randomizr) | first %}
  {% set randomWidth2 = shuffle(randomizr) | first %}
  
  {% set randomHeight3 = shuffle(randomizr) | first %}
  {% set randomWidth3 = shuffle(randomizr) | first %}
  
  {% set randomHeight4 = shuffle(randomizr) | first %}
  {% set randomWidth4 = shuffle(randomizr) | first %}
  
  {% set randomHeight5 = shuffle(randomizr) | first %}
  {% set randomWidth5 = shuffle(randomizr) | first %}
  
  &lt;div class="sandbox"&gt;
      &lt;div style="left: {{ randomHeight1 }}%; top: {{ randomWidth1 }}%;"&gt;&lt;/div&gt;
      &lt;div style="left: {{ randomHeight2 }}%; top: {{ randomWidth2 }}%;"&gt;&lt;/div&gt;
      &lt;div style="left: {{ randomHeight3 }}%; top: {{ randomWidth3 }}%;"&gt;&lt;/div&gt;
      &lt;div style="left: {{ randomHeight4 }}%; top: {{ randomWidth4 }}%;"&gt;&lt;/div&gt;
      &lt;div style="left: {{ randomHeight5 }}%; top: {{ randomWidth5 }}%;"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Is this somehow possible without writing each variable manually?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-05T14:09:44.893Z
Comments.Text: I don't know what you're creating, but does it make sense to explore changing these values in JavaScript rather than directly in the markup?
Comments.UserDisplayName: Gavin Anderegg
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-07T08:52:29.600Z
Comments.Text: Yeah I taught about that too but first I'd like to see if it's possible within twig.
Comments.UserDisplayName: KSPR
Tags: <array><range>
Title: Random Number from range
ThreadText: # Random Number from range
  
  (Asked by: username_0 on 2016-08-05)
  
  What is the best way to get a random number within a certain range?
  
  I made something but I guess there is a better way:
  
  In this example I want to position some elements randomly. This approach works, but I have to define a variable for each value.
  
  ```
  {% set randomizr = range(10, 90) %}
  
  {% set randomHeight1 = shuffle(randomizr) | first %}
  {% set randomWidth1 = shuffle(randomizr) | first %}
  
  {% set randomHeight2 = shuffle(randomizr) | first %}
  {% set randomWidth2 = shuffle(randomizr) | first %}
  
  {% set randomHeight3 = shuffle(randomizr) | first %}
  {% set randomWidth3 = shuffle(randomizr) | first %}
  
  {% set randomHeight4 = shuffle(randomizr) | first %}
  {% set randomWidth4 = shuffle(randomizr) | first %}
  
  {% set randomHeight5 = shuffle(randomizr) | first %}
  {% set randomWidth5 = shuffle(randomizr) | first %}
  
  <div class="sandbox">
      <div style="left: {{ randomHeight1 }}%; top: {{ randomWidth1 }}%;"></div>
      <div style="left: {{ randomHeight2 }}%; top: {{ randomWidth2 }}%;"></div>
      <div style="left: {{ randomHeight3 }}%; top: {{ randomWidth3 }}%;"></div>
      <div style="left: {{ randomHeight4 }}%; top: {{ randomWidth4 }}%;"></div>
      <div style="left: {{ randomHeight5 }}%; top: {{ randomWidth5 }}%;"></div>
  </div>
  
  ```
  
  Is this somehow possible without writing each variable manually?
  
  **Tags:** array, range
  
  > **Comments:**
  > **username_1**: I don't know what you're creating, but does it make sense to explore changing these values in JavaScript rather than directly in the markup?  
  > **username_0**: Yeah I taught about that too but first I'd like to see if it's possible within twig.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_4 on 2016-08-05. Score: 4 | Accepted answer
  
  Building off of username_2's answer, you might consider using a `for` loop to render your markup, instead of setting all of those variables:
  
  ```
  {% set range = range(10, 90) %}
  {% set totalDivs = 5 %}
  
  <div class="sandbox">
    {% for i in 1..totalDivs %}
      <div style="left: {{ random(range) }}%; top: {{ random(range) }}%;"></div>
    {% endfor %}
  </div>
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2016-08-05. Score: 2
  
  Instead of using [shuffle](http://twig.sensiolabs.org/doc/extensions/array.html), which is only available in the extra [Array extensions library](http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install), you could just use [random](http://twig.sensiolabs.org/doc/extensions/index.html#extensions-install).
  
  ```
  {% set range = range(10, 90) %}
  
  {% set randomHeight1 = random(range) %}
  {% set randomWidth1 = random(range) %}
  
  {% set randomHeight2 = random(range) %}
  {% set randomWidth2 = random(range) %}
  
  ...
  
  ```
  
  > **Comments:**
  > **username_3**: There is this crazy cool CMS named Craft, that adds some Twig functions on top of the default ones and [`shuffle`](https://craftcms.com/docs/templating/functions#shuffle) is one of them! ;-)  
  > **username_2**: I'll have to check it out sometime! :)  
  
  ---
  
  