# Power Automate: How to Generate a Random Number

> For the full documentation index, see https://250highstreet.com/llms.txt

> Use rand() in Power Automate to get a random whole number. The top value is never returned. Pick a random item from a list and add leading zeros.

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.

1.  Add a Compose action, or select the field that needs the number.
2.  Open the Expression tab.
3.  Enter rand(1, 101) to get a whole number from 1 to 100.
4.  Select Add, then save the flow.
5.  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](/guides/power-automate-get-items-only-returns-100/) 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](/guides/power-automate-invalid-type-expected-string-but-got-null/) and [the empty field guide](/guides/power-automate-condition-field-empty/).

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.

## Sources

- [Reference guide to workflow expression functions: rand (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference)

## Related guides

- [Power Automate Get items stops at 100 rows: how to get them all](/guides/power-automate-get-items-only-returns-100/)
- [Fix "Invalid type. Expected String but got Null" in Power Automate](/guides/power-automate-invalid-type-expected-string-but-got-null/)
- [Power Automate condition to check if a field is empty](/guides/power-automate-condition-field-empty/)
- [Power Automate: Generate a Unique ID With guid()](/guides/power-automate-guid-generate-unique-id/)

Source: https://250highstreet.com/guides/power-automate-random-number-rand/
