cancel
Showing results for
Did you mean:
Helper III

## 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
Community Champion

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

Community Champion

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.

Helper III

Thanks a lot for your help, it works !

Announcements

#### Power Platform Connections Ep 14 | J. Panchal | Thursday, 18 May 2023

Episode Fourteen of Power Platform Connections sees David Warner and Hugo Bernier talk to Microsoft PM Jocelyn Panchal, alongside the latest news, videos, product reviews, and community blogs.   Use the hashtag #PowerPlatformConnects on social media for a chance to have your work featured on the show.      Show schedule in this episode:  00:00 Cold Open 00:32 Show Intro 01:10 Jocelyn Panchal Interview 24:10 Blogs & Articles 29:50 Outro & Bloopers  Check out the blogs and articles featured in this week’s episode:   https://www.nathalieleenders.com/Blog/index.php/;focus=STRATP_com_cm4all_wdn_Flatpress_42136159&path=?x=entry:entry230511-101930#C_STRATP_com_cm4all_wdn_Flatpress_42136159__-anchor  @NathLeenders https://www.keithatherton.com/posts/2023-05-12-msbuild2023-cloud-skills-challenge/  @MrKeithAtherton https://elliskarim.com/2023/05/13/how-to-find-files-in-onedrive-that-match-a-naming-pattern/  @MrCaptainKarim https://www.linkedin.com/pulse/my-fond-memories-scottish-summit-2022-pranav-khurana/ @pranavkhuranauk https://www.linkedin.com/feed/update/urn:li:activity:7061777660745560064/?updateEntityUrn=urn%3Ali%3Afs_feedUpdate%3A%28V2%2Curn%3Ali%3Aactivity%3A7061777660745560064%29  @thevictordantas  Action requested: Feel free to provide feedback on how we can make our community more inclusive and diverse.  This episode premiered live on our YouTube at 12pm PST on Thursday 18th May 2023.  Video series available at Power Platform Community YouTube channel.  Upcoming events:  Power Apps Developers Summit – May 19-20th - London European Power Platform conference – Jun. 20-22nd - Dublin Microsoft Power Platform Conference – Oct. 3-5th - Las Vegas  Join our Communities:  Power Apps Community Power Automate Community Power Virtual Agents Community Power Pages Community  If you’d like to hear from a specific community member in an upcoming recording and/or have specific questions for the Power Platform Connections team, please let us know. We will do our best to address all your requests or questions.

#### Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/

#### Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.       Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.    Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.