cancel
Showing results for
Did you mean:
Resolver I

Create button to generate random numbers

Hi all!

I would like to create a button to generate numbers from 0001 to 9999.

And then the number appears in a text input. This is connected to a Sharepoint List.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Super User

Put this code in the OnSelect property of the button.

``````Set(randLowest, 1);
Set(randHighest, 9999);
Set(randBetween, RoundDown(Rand()*(randHighest-randLowest + 1)+randLowest,0))``````

Then put this code in the Default property of the Text Input

``Text(randBetween,"0000")``

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Community Champion

Hi @bsoares ,

Since you want it in 0001 to 9999 format, Use the below formula for input text to add zeros to the meet the same format.
Set this formula on Inputtext control:

``````If(
Len(Text(VarRandValue)) = 1,
Concatenate(
"000",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 2,
Concatenate(
"00",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 3,
Concatenate(
"0",
Text(VarRandValue)
),
Text(VarRandValue)
)``````

On button click set the formula as:

``UpdateContext({VarRandValue:(RoundDown(9999 * Rand(), 0))})``

This will take the numbers from 0 to 9999.

Please have a look in the working model you can see preceding zeros for the number which are less than 4 digits:

I hope this resolved your issue if you see any challenge let me know I am always happy to help.

Regards,

Krishna
If this post helps give a 👍 and if it solved your issue consider Accept it as the solution to help the other members find it more.

I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
KrishnaV
Business Applications MVP, Microsoft Certified Trainer
If this post helps you give a THUMS-UP and if it solved your issue consider Accept it as the solution to help the other members / new members of the community.

4 REPLIES 4
Super User

Have you seen the Rand() function?  Documentation is here: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-rand

Super User

Put this code in the OnSelect property of the button.

``````Set(randLowest, 1);
Set(randHighest, 9999);
Set(randBetween, RoundDown(Rand()*(randHighest-randLowest + 1)+randLowest,0))``````

Then put this code in the Default property of the Text Input

``Text(randBetween,"0000")``

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Community Champion

Hi @bsoares ,

Since you want it in 0001 to 9999 format, Use the below formula for input text to add zeros to the meet the same format.
Set this formula on Inputtext control:

``````If(
Len(Text(VarRandValue)) = 1,
Concatenate(
"000",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 2,
Concatenate(
"00",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 3,
Concatenate(
"0",
Text(VarRandValue)
),
Text(VarRandValue)
)``````

On button click set the formula as:

``UpdateContext({VarRandValue:(RoundDown(9999 * Rand(), 0))})``

This will take the numbers from 0 to 9999.

Please have a look in the working model you can see preceding zeros for the number which are less than 4 digits:

I hope this resolved your issue if you see any challenge let me know I am always happy to help.

Regards,

Krishna
If this post helps give a 👍 and if it solved your issue consider Accept it as the solution to help the other members find it more.

I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
KrishnaV
Business Applications MVP, Microsoft Certified Trainer
If this post helps you give a THUMS-UP and if it solved your issue consider Accept it as the solution to help the other members / new members of the community.

Resolver I

Thanks a lot for your help, it works !

Announcements

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!