<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>

CC BY-SA 3.0
2016-08-06T15:07:29.720Z
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! ;-)
carlcs
CC BY-SA 3.0
2016-08-06T15:11:52.103Z
I'll have to check it out sometime! :)
Brad Bell
CC BY-SA 3.0
2016-08-05T17:21:34.033Z
2016-08-05T17:21:34.033Z
Brad Bell
<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>

CC BY-SA 3.0
2016-08-05T20:06:05.590Z
2016-08-05T20:06:05.590Z
Mats Mikkel Rummelhoff
<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>

CC BY-SA 3.0
2016-08-05T14:09:44.893Z
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?
Gavin Anderegg
CC BY-SA 3.0
2016-08-07T08:52:29.600Z
Yeah I taught about that too but first I'd like to see if it's possible within twig.
KSPR
<array><range>
Random Number from range
# 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 %}


    
    
    
    
    


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 Brad'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 %}


  {% for i in 1..totalDivs %}
    
  {% endfor %}


---

### Answer by username_2 on 2016-08-05. Score: 2

Instead of using shuffle, which is only available in the extra Array extensions library, you could just use random.
{% 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! :)  

---

