Randomization plays a massive role in our lives, even if we rarely see it. Power Automate has excellent support for randomization with the “rand” function that allows us to extract a random number within a range.
So today, let’s look at where we can find it and how to use it.
Where to find it?
You can find the function in every action where a formula is supported. For example, let’s look at a “Compose” action:
As you can see, we can auto-complete by using the “tab” key. First, let’s look at how to use it.
It follows a simple pattern.
- Number to start
- Number to end
Let’s start with a simple example:
rand(1,10) will return 6
Since we’re talking about randomization, your result can differ, but that’s the whole point.
Please note that the result will be from the min value including until the last value excluding. So in the example above, we will never get “10”.
What if we provide a float value like this?
We will get the following:
Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'rand' expects its first parameter 'minimum' to be an integer. The provided value is of type 'Float'. Please see https://aka.ms/logicexpressions#rand for usage details.'.
Notice that Power Automate explicitly states that the randomization only works for integer values, so float values are not supported.
Finally, let’s take a look at negative numbers. Can we randomize them?
We’ll get a random number, even if it’s negative, so if you need negative random numbers, you’re in luck.
Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise that you break it into smaller manageable formulas.
The max value for the integer needs to be lower than 2147483647. Otherwise, you’ll get the following error message.
Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'rand' parameters are out of range: the value '1' provided for 'minimum' cannot be greater than the value '-2147483648' provided for 'maximum'. Please see https://aka.ms/logicexpressions#rand for usage details.'.
This number looks random (no pun intended), but it’s not. Since we’re dealing with integer values, this is the max value possible for a 32-bit signed integer. Still, if you need to randomise numbers higher than these, use other frameworks or tools to get an accurate randomization factor.
Common Mistakes using the “rand” function
I’ve mentioned this before, but there are two mistakes that I see a lot.
The second parameter is not included
For example, when you want to find a value from 1 to 10, please always include the parameter with a +1 value. Then, if you’re getting the value dynamically, you can use the “add” function to add one value to the dynamic value and get the range you need.
Only integer values
This function only supports randomization for integer values, so be aware of this when providing values to the “rand” function. You can give negative numbers as the range, however, being the limit from -2147483647 to 2147483647, as mentioned above.
Here are some things to keep in mind.
There’s no real reason to do it, but if you find yourself in a situation where you have nested “rand” functions in a formula, you should review it and make everything more straightforward. For example, it doesn’t make much sense to have a random number as the range for another randomization, but if this happens, please send me an email because I would love to include that example here for everyone to see.
Always add a comment.
Adding a comment will also help avoid mistakes. Indicate why you are trying to find the random element and what it means. It may look obvious initially but it will not be in a few months or years. So it’s essential to enable faster debugging when something goes wrong.
Back to the Power Automate Function Reference.