web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Create button to gener...
Power Apps
Answered

Create button to generate random numbers

(0) ShareShare
ReportReport
Posted on by

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.

Thanks in advance!

2020-05-30 20_31_19-Test1 - Saved (Unpublished) - Power Apps.png

Categories:
I have the same question (0)
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @bsoares 

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

  • Verified answer
    mdevaney Profile Picture
    29,991 Moderator on at

    @bsoares 

    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."

  • Verified answer
    KrishnaV Profile Picture
    5,023 on at

    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:

    Rundown function.gif

     

    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.

     

  • bsoares Profile Picture
    on at

    @KrishnaV @mdevaney 

     

    Thanks a lot for your help, it works !

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 493 Most Valuable Professional

#2
11manish Profile Picture

11manish 479

#3
Haque Profile Picture

Haque 328

Last 30 days Overall leaderboard