November 17, 2024

How to access a random element in an array in Power Automate?

I got this question in Power Automate’s community, and the person wants to access a random element in an array so that they can extract a string from it. Here’s the question:

I am aware of rand() function for numeric values. My idea is to have a function that randomly select certain phrases from a predefined array:

Array’HelloWorld’, ‘Hi’, ‘Are you there?’, ‘Tip of the day’

Output is randomized on each run

First Run: Hi

Second Run: Tip of the day

Third Run: Hi

It’s not as hard as it looks, but we must consider some small details; overall, we can have an array with all the elements that we want, and since we can access the elements of the array dynamically, we can then generate the index of the element that we want to access using the randomization function that Power Automate provides us. Here’s the global overview of our approach:

It’s not complex, but let’s see it step-by-step. First let’s build the array.

Build the array

Let’s see how to build the array with the following values:

'HelloWorld', 'Hi', 'Are you there?', 'Tip of the day'

Power Automate provides us with a nice function called “createArray” that will get several parameters and return an array with all of them so that we can do the following:

createArray('HelloWorld', 'Hi', 'Are you there?', 'Tip of the day')

We will use a variable, and the “Initialize variable Action” to do it.

Now that we have an array, let’s see how to access it.

Access the array

We can access the array using the index of the element we want. For example, if we want the first element, we would use index 0 since the count starts at zero when referring to the arrays. So we could do the following:

<item with the array>[<index that we want to access>]

The square brackets will indicate to Power Automate that we want to access a position inside an array, so we only need to provide an index, and we’re good to go.

It is essential to know how to access it since it will influence how we generate the random value. Let’s see how to do it.

Get the random value

How that we know how to access a single element in an array? Let’s look at how to randomize the index. To do that, we will use the “rand” function with a start and end value for the randomization. For example, since we have four elements in the array, we can easily build the “rand” function as follows:

rand(1,5)

This will return random values between 1 and 5, introducing a problem. If we add more elements to the array, we need to remember to change the random function; otherwise, the new elements will never be considered. So it’s better to always do the calculations based on the number of elements in the array, and then we can safely add or remove elements without any impact on the Flow. To do that, we can use the “length” function as follows:

rand(1,length(variables('ARRAY')))

Now we have a random index, let’s find the element in the array.

Fetch the element

Now that we have an index, let’s find it in the array. Remember the previous section where we discussed that the index starts at zero, not one, so we need to decrement the index using the “sub” function. It will look like this:

variables('ARRAY')[sub(outputs('Random_number'),1)]

I know what you’re thinking. Why not make a random value from 0 to the length, and then we would not need to do the sub here? The answer is that you always need to do the sub since the “length” function will return the number of values of an array. You can indeed do the following:

rand(0,sub(length(variables('ARRAY'),1))

And access it as follows:

variables('ARRAY')[outputs('Random_number')]

It’s about where you want to put the subtraction, and the result is the same as long as you do it.

Here’s the final result:

Final thoughts

The solution to get the random element is quite simple. Generate an array, get a random index and get the value at that index. I wanted to show you how to do it step by step to ensure that you understand what issue with the subtraction and how to get the value from the array.

I like this question because it demonstrates well how to take advantage of the native capabilities of Power Automate to solve complex problems efficiently.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and check out other Power Automate-related articles here.

Photo by Jess Bailey on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon