How to generate a random number in Power Automate
Use the rand() expression. Write rand(1, 11) to get a whole number from 1 to 10. The first number is the lowest result. The second number is one more than the highest result, so it is never returned.
What does the rand() function do?
The rand() function returns a random whole number from a range. Microsoft documents it as rand(minValue, maxValue). The range includes the start value and excludes the end value.
Microsoft gives rand(1, 5) as its example. That call returns 1, 2, 3 or 4. It never returns 5. To include the highest number you want, add 1 to the second value.
How do you add a random number to a flow?
Run the flow several times. The output changes on each run.
- Add a Compose action, or select the field that needs the number.
- Open the Expression tab.
- Enter rand(1, 101) to get a whole number from 1 to 100.
- Select Add, then save the flow.
- Run the flow and check the Compose output in run history.
How do you pick a random item from a list?
Count the items with length(), then use the result as the index. Write outputs('Get_items')?['body/value'][rand(0, length(outputs('Get_items')?['body/value']))] with your own action name.
Arrays start at position 0. The range rand(0, length) therefore returns every valid position and nothing beyond the end. If the list is empty, the call fails, so check the length first with a Condition.
A Get items action returns up to 100 items by default. Read the guide on the 100 item limit if your list is longer.
How do you add leading zeros to a random number?
The rand() function returns a number, so it drops leading zeros. To make a six digit code, write formatNumber(rand(0, 1000000), '000000'). The formatNumber() function returns text.
Do not use this for passwords or security codes. Microsoft does not state that rand() is built for security use. Use a service made for that purpose.
Why does the expression fail or return the same value?
If the first number is larger than the second, the flow can fail. Put the lower value first.
If a value comes from a dynamic field, it can be empty or text. Convert it with int() and guard empty values. Read the null input guide and the empty field guide.
If you need a unique ID rather than a number, use guid(). It gives a value that is very unlikely to repeat.
FAQ
Is the maximum value included in rand()?
No. The second value is one more than the highest result. rand(1, 5) returns 1, 2, 3 or 4.
Does Power Automate have a random number action?
Not as a separate action. Use the rand() expression in a Compose action or in a field.
Can rand() return a decimal?
No. It returns a whole number. Write div(rand(0, 100), 100.0) to get a value from 0.00 to 0.99.
How do I get a random item from a SharePoint list?
Use Get items, then use rand(0, length(items)) as the index into the returned array.
Should I use rand() for passwords?
No. Use a tool built for secure codes.