How to get random ITEMS with Power Automate
How to get random ITEMS with Power Automate
In this post, you will learn how to get random ITEMS with Power Automate.
I think it is easy to get the single random item (I will demonstrate that also). Then I started to ask myself:
- What to do to get 3 or 5 random unique items from an array (list)?
- What if I would need to get a random number of items from an array (list) where items are not in sequence (some items were deleted, and the IDs are not one after another)?
During reading, I will also explain other functions and concepts, so you will not only learn one thing.
I hope that sounds interesting, so let’s start, How to get random ITEMS with Power Automate.
Table of content:
- Rand function explained
- Get one random item
- Get random items from Array
- Get unique random items
- Get items from SharePoint list
- Summary
Rand function explained
First things first, I will be using rand function, but I would like to make sure that it is understood correctly (for me, it was not so obvious).
Before moving to the explanation, I will make a small experiment.
Experiment
I created a simple Cloud Flow which have the following steps:
- Initialize variable varResults of String type whci will be used to show results of Flow execution
- Do Until action which will execute 10 times (set limit), if you want to learn more about this action and how it is working, check the post where I explained Do Until
- Inside the Do Until, I added the action of Append to string variable (varResults) to add the results of the rand function:
rand(1,6)
- Last is Compose action where I added varResults variable

NOTE: I share this Flow in my GitHub and you can download it.
Result:

As you can see (I run this Flow couple times) number 6 thou it was part of the array never showed in the results…
Explanation
I think now I’m ready to explain how the rand function is working. I will use my experiment configuration:
rand(1,6)
Rand function works based on the range between the minimum and maximum arguments.
The first argument is the minimum (in my example have value of 1) and is included in the results!
The second argument is the maximum (in my example have value of 6) however is NOT included in the results!
I will now prove that maximum, second argument is NOT included by changing the rand configuration to:
rand(1,7)
Results:

TIP
To add rand function, simple select expressions (next to Dynamic Content) and start to type in the name of the function, after it add opening bracket (designer will add closing bracket) and set the minimum, maximum arguments:

I hope, I manage to explain the rand function correctly. I will now move to the next step and show to work with rand function and array / list of items.
Get one random item
To demonstrate how to get one random item from the array I created a new Flow:

NOTE: I share this Flow in my GitHub and you can download it.
- First of all I’m creating Array variable – varArray:
[ "A", "B", "C", "D", "E", "F"]
- Second action is compose, (I added it only for testing, in the production I would use normal variable):
variables('varArray')?[rand(0,6)]
As the result after each execution, I’m getting random element of my array, for instance:

Now let me explain what is happening here.
Array indexing
In the Power Automate (and not only), I can get the specific item of array by providing its location. That location is also known as the Array indexing. To get a specific item just provide the value of the index in array:
arrayName?[ValuOfIndex]
Above all, the key for me was to get that Array indexing / locations is starting with ZERO.
Based on my previous example of array:
[ "A", "B", "C", "D", "E", "F"]
If I want to get the first item (“A”) I need to provide ZERO value of Array index:
varArray?[0]
Similarly, for the third item (“C”) I need to type:
varArray?[2]
The last one (“F”) will be:
varArray?[5]
Now I will connect that with rand function.
variables('varArray')?[rand(0,6)]
I manage to show that the rand function returns a single value from a data range. By setting the minimum to zero, I know that first items will be returned from my varArray. By setting the maximum to 6 I knew that rand result might be up to a value of 5. As a result, I covered the whole range to get any possible result from my array.
In conclusion, if we work with Array index, it is easy to get one random item.
Get random items from Array
To demonstrate how to get random items, I modified my FIRST experimental flow by adding to it one variable with array of value between 1-6:

Next in the Append to string variable action I modified the formula:
variables('varArray')?[rand(0,length(variables('varArray')))]

NOTE: I share this Flow in my GitHub and you can download it.
I will break it down step by step, first let’s check rand function.
As the maximum argument in the rand function, I used the length function.
Length function is returning number of items from the array (for this example = 6).
I will pause here for a second. Similarly to my previous example, the length of array is 6. Thanks to the fact that Array indexing is starting with zero by providing array length in maximum argument, I’m certain that ALL items are in the range of rand function! As the last item in the array is having Array index equals length minus one (max argument is not included in rand results).
If I made this part unclear, please let me know, I am happy to try to explain it better.
As the minimum argument of Rand function, I set as ZERO of course.
In short, below function:
variables('varArray')?[rand(0,length(variables('varArray')))]
Is getting the random Array index from the whole range of the array.
Thanks for keeping Do Until function I could repeat the selection from array as many times as I set loop limit.
In addition, while using Array indexing I don’t need to worry about the order of items or if items were deleted. Array indexing is an answer to one of my questions.
I will prove it by changing the varArray to:
varArray - [2,6,1,8,3,7]
I still have an array of 6 items, length is 6 and in the results you will see only values from varArray:

Benefit of length function
I know that I could set the maximum of the rand function as static value. But that would be good only for testing. In real life scenarios, I don’t know how many items will be returned.
Length function return number of items and thanks to that I’m sure that my maximum argument in rand function will be always correct. In other words, making my Flow smarter and more flexible.
I think the general idea is in place of how to get items from array.
So far I showed how to extract items from array. However, results were repeating and in the real world that does not make any sense. In other words now I should show how to avoid such situations.
Get unique random items
I’m closer to the end and the Flow is evolving again:

NOTE: I share this Flow in my GitHub and you can download it.
Actions and their explanation in order from top to bottom:
- Init varArray it keeps the varArray data:
varArray - [2,6,1,8,3,7]
- Init varResults will keep the results of the Do Unitl loop, used to show the value of selected items of varArray
- Do Until loop currently set to run 3 times
- GetItem is Compose action where I inserted function:
variables('varArray')?[rand(0,length(variables('varArray')))]
- Append to string variable is keeping the Outputs / result of the GetItem (Compose) action
- Filter array is an action which allows creating smaller set of data based on the condition. For me, I’m using it to remove a selected item from varArray. [I will explain this shortly.]
- Set variable is where I’m making the update of varArray with Body value of Filter array action. As the result, next iteration of Do Until will work with new / updated varArray [again it is showing how length function is helping].

- Final step is just Compose action where I’m displaying data of varResults
Filter array step explained
As I mentioned, Filter array allows me to remove an item which was “selected” by the rand function.
Configuration of Filter array:

- From – I’m adding here varArray so the array where I’m doing the “search”
- Condition
- Left side is the Expression of item()
- is not equals to
- Right side, the Output of the GetItem (Compose) action . Basically, this is the result of the function:
variables('varArray')?[rand(0,length(variables('varArray')))]
As a result of the above configuration, each item which is not the one “selected” by rand function will be returned. For instance:

And this is it, how to get the unique items from the array.
Making number of selected items flexible
To make this process more flexible, I would like to have control how many items will be selected. To achieve this, I will add a parameter in trigger to control Do Until execution:

Get items from SharePoint list
I cannot close this post yet, as I would like also to demonstrate how to use presented approach with real data. For example, I will use SharePoint list:

NOTE: I share this Flow in my GitHub and you can download it.
Actions which I modified in this version of the Flow:
- I added Get Items action which connected to basic SharePoint list
- Select item action to make the number of returned properties easier to track:

- Next, I added Compose action with length function to check the number of items in both Get items, select actions and to show that number of the results match:

- In the Init varArray I’m inserting Output property of the Select action
- The last modification which I made was in GetItem (Compose) action:
variables('varArray')?[rand(0,length(variables('varArray')))]?['ID']
I added question mark (?) and [‘ID’] to get the value of ID of the selected item. The [‘ID’] can be replaced with [‘Title’] or any other property which should be extracted.
Yet, if the single property is not required instead using action Append to string variable I could also change the action to Append to array variable and “capture” whole item. If with this statement I created confusion, please let me know I will add further clarification.
TIP
Question mar (?) is really important as with it if there is no item or request property does not have value Flow will still work. Without it, process may fail.
After execution of the Flow and requesting 5 Items I see IDs of the random Items:

Summary
First of all thank you for reading as I was not expecting that How to get random ITEMS with Power Automate will be so long. I hope in this journey I manage to explain not only above topic but also a couple of others:
- rand function
- length function
- Array indexing
- Filter array
I’ m also very happy to share with you my GitHub and Flows used as the examples. Hope it will be useful and help you with your work.
And if I’m speaking about the work, I’m using above functions and actions in a lot of my Flows. But How to get random ITEMS with Power Automate itself is also very useful. Image that you need to send random questions to check User satisfaction. Or you need to send the questions to random Users? There is many use cases where it can be used. I hope you will write to me a comment where you are using this method.
Wish you a great day!