# Is it possible to set a variable to a random number from an array?

**URL:** <https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171>\
**Category:** Questions & Help\
**Created:** [December 26, 2019, 10:33pm UTC](https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171 "2019-12-26T22:33:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesper\_Madsen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jesper_madsen/32/10013_2.png) [@Jesper\_Madsen](https://community.homey.app/u/Jesper_Madsen)\
**Post date:** [December 26, 2019, 10:33pm UTC](https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171/1 "2019-12-26T22:33:12Z")

</div>

Hello,  
I have a few flows involving sounds and a variable (SoundVar) that determines which sound to play:

- If SoundVar is 1 then play song one
- If SoundVar is 2 then play song two  
…  
I have 5 of these flows.  
I can pretty easily calculate a random number to put in SoundVar with {{round(random(1,5))}} when an action happens.

My question is, can you do this if specifying which numbers to randomize ?  
Example:  
When pressing button 1 I want to use sound 1,2,3,4 and 5.  
When pressing button 2, I want to use sound 2,4 and 5 but not 1 and 3.  
For my doorbell I want to use sound 1,3 and 4 but not 2 and 5.

Is there a one-liner that one can put into the "calculate variable as … " that can do this?

Edit: I would like to say that this is just a small example of a larger picture and for this few flows, I would probably just create different variables and flows for each device. It is going to be larger and encompass other things than just sounds, meaning that I could use it for other types of flows as well.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 27, 2019, 6:02am UTC](https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171/2 "2019-12-27T06:02:20Z")

</div>

It’s in Dutch but it can help what you want to achieve. Google translate is your best friend  
[Link](https://community.homey.app/t/willekeurig-radiozenders-afspelen/18532)

---

<div class="post-metadata">

**Author:** ![Jesper\_Madsen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jesper_madsen/32/10013_2.png) [@Jesper\_Madsen](https://community.homey.app/u/Jesper_Madsen)\
**Post date:** [December 27, 2019, 10:07am UTC](https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171/3 "2019-12-27T10:07:01Z")

</div>

Thank you for the answer.  
Don’t get me wrong here. This method is viable.  
But I feel like it is only to a certain degree. Specifically it is limited by the maximum of 3 in “And/or”.

For others reading: I would create a new variable for each device that would trigger the sounds and have different “playlists” and would then set it up in each flow for the sounds in the “And/or” section.

Example: For the doorbell above (should play 1,3,4), I would make a new variable called “DoorBellSounds”.

- In the doorbell flow, I would make a card with “Set DoorBellSounds as {{round(random(1,3))}}”
- In each of the flows for Sound One, Sound Three and Sound Four I would add a card in the “And…” section to trigger it if SoundVar is one number or if DoorBellSounds is another number.

I know that I am being picky here, but the reason for the random with an array of numbers is that this could be solved by a single card if it is possible.

For clarity’s sake I can try to give an example even though I know **it is not a real calculation** :

It would be something like _**{{random([1,2,4])}}**_ that would provide a random number of either 1,2, or 4.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 27, 2019, 2:18pm UTC](https://community.homey.app/t/is-it-possible-to-set-a-variable-to-a-random-number-from-an-array/23171/4 "2019-12-27T14:18:44Z")

</div>

> [@Jesper\_Madsen](#):
>
> It would be something like _**{{random([1,2,4])}}**_ that would provide a random number of either 1,2, or 4

It could be like this {{round(random(1-4),0)}} in that case you als have to make a extra flow, as it becomes number 3 start te flow again
