cancel
Showing results for
Did you mean:
Helper III

## Draw random numbers

Hello everyone.
I have three text input boxes, where the boxes are defined as numbers. There are "min" numbers, "max" numbers and "quantity" numbers. When I press the random button, I create a table, with numbers from the text input boxes "min" and "max". Then I draw a random "quantity it is displayed in a gallery.

Then comes the question.
I want to press the random button once and the first random number should appear in a Label on the screen. Then I should be able to press the next button and the next number will appear in Label, and previous numbers will be shown in the gallery. Is this possible to achieve in a decent and simple way?
Attaches coding to the Random button. The formula is in the Norwegian setup of power apps. A comma becomes a semicolon and a semicolon becomes a double semicolon.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Super User

OOOOOOOH OK Makes sense. Ok so Single number box

Last(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), winningNumbers))

mulitbox below it

Concat(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), winningNumbers-1), Value, " ")

then you can add a little to that all numbers box to show when all numbers have drawn say like

Switch(winningNumbers,
Value(TextInput3.Text),
Concat(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), winningNumbers), Value, " "),
Concat(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), winningNumbers-1), Value, " ")
)

and even set visible of the solo number box to

Switch(winningNumbers,
Value(TextInput3.Text), false, true)

adding in those last two will make it display one at a time and then move into the group as you continue until the end, at which point all numbers will show in the group box

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Super User

watched your vide, looks lovely. Reason it just goes straight to the box below is the last part we added.

Switch(winningNumbers,
Value(TextInput3.Text), false, true)

change visible of the single text box from that, to true, and it will stay there 😉

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
15 REPLIES 15
Super User

Where I have 3 text inputs, set to numbers, named TextInput 1 2 3, in my buttons onselect I place

ClearCollect(numbersDraw,
ForAll(Sequence(Sum(TextInput2.Text+1-TextInput1.Text),TextInput1,1) As numberTurn, {Value:numberTurn.Value, Random:Rand()}))

In my label I place

Concat(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), TextInput3.Text), Value, " ")

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper III

My formula works on the random button, but I want to add a new next button.

I want to press the random button once and the first random number should appear in a Label on the screen. Then I should be able to press the next button and the next number will appear in Label, and previous numbers will be shown in the gallery.

The ultimate would be that now that the last number has been drawn, a text will appear saying "No more moves"

Super User

If I understood correctly, you want to

1) Press Random once > then Next once (?) > then a text saying "No more moves"

2) Display the current value in a label, and the previous in a gallery

You can use a variable to capture the button presses.  Add something like this to the "OnSelect" for your Random and Next buttons:

UpdateContext({varButtonPush: varButtonPush + 1})

You can then reference this for your buttons in the DisplayMode property.  For example, for the "Random" button you can add:

If(varButtonPush > 0, DisplayMode.Disabled)

So you can disable your buttons as the user advances. For the "No More Moves", you can set the visible property to

If(varButtonPush = 2,true,false)

For the Gallery portion, you can capture the previous value into a collection, and then use that for your Gallery.   So if the current value is in your label, before you replace that with a new value, add this to the beginning of your formula:

Collect(colPriorRandom,YourTextLabel.Text)

You'll want to clear the collection at some point, but these are just a few ideas that may work for your use case

Super User

So, I would still make your random button different, as yours is far more complex than necessary and will make future edits harder if someone that isn't you has to work with this code one day.

To test, make a button with the onselect of

If(IsBlank(winningNumbers),
ClearCollect(numbersDraw,
ForAll(Sequence(Sum(TextInput2.Text+1-TextInput1.Text),TextInput1,1) As numberTurn, {Value:numberTurn.Value, Random:Rand()})));

Set(winningNumbers, If(IsBlank(winningNumbers),1, winningNumbers+1))

and set its displaymode to

If(Not(winningNumbers=Value(TextInput3.Text)), DisplayMode.Edit, DisplayMode.Disabled)

and it's text to

If(Not(winningNumbers=Value(TextInput3.Text)), "Draw Number", "Draw Complete")

then put a label in the screen with this text

Concat(FirstN(Sort(numbersDraw,Random,SortOrder.Ascending), winningNumbers), Value, " ")

this will draw one number at a time until the requested limit is reached, then chaneg your button from draw number to draw complete and make it unable to be pressed further. Can then reset it by placing this somewhere, if needed

Set(winningNumbers,Blank())

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper III

Entered what you wrote and got this? Unable to press.

Super User

Check your display mode of the button and make.sure you changed the textbox3 reference to name of your own textbox where your draw count is located. And if it is correct then place a label somewhere and see what value is currently in winningNumbers (also check that all my formula textbox references where swapped when you put them.in your app so you dont end up with a reference throwing it off 🙂 )

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper III

Typo on the text box. Thanks. This works well.
What if I want to show only one feature in a new text box, and that all are collected in the one I have now.

Super User

Do you mean you want a seperate text box for each number revealed by the draw?

_____________________________________________________________________________________
Like my answer? - Hit that Thumbs Up. Resolved the Issue? - Hit Accept as Solution.
This helps others find solutions to future issues!
Helper III

No, not separately. A textbox. The first number appears in and the next number appears in the same text box and the first goes into the text box that is there now. If this makes sense

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. Our team will be reviewing posts using the new "Copilot" label to ensure we highlight and amplify the most relevant and recent content, so you're assured of high-quality content every time you visit. If you share a post that gets featured in the curated gallery, you'll get a PM in the Community to let you know!The curated gallery is ready for you to experience now, so visit the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community. We can't wait to see what you "cook" up!