Having trouble adding random number to a list of things

Is there any way to use either the random number generator plugin or the built in calculate formula—>generate random string function to add a random number to a group of things? I am going crazy trying to figure this out!

Here’s a little bit of background:
I’m creating a number of things using the Schedule API Workflow on a List function. It is using a list of users from a repeating group to create a new thing for each of them. If I set up the workflow to only create 1 thing at a time, it is easy to pass the random number generator into the new thing. But I really need to be able to create multiple things all in one shot, which is why the API workflow is key. However, I cannot figure out how to pass random numbers into the API workflow to be used in an appropriate manner. I can create a list of random numbers that is the right number of numbers (for example, if I’m creating 3 things I can create a list of 3 random numbers via a custom state). And, I can even pass this list into the API workflow. However, the API workflow does not properly iterate through this list–it gives them the same number even if I set up an indexer. If I pick a random thing off the list (and the list is only a few items long), there is a good probability of having duplicate values, which is what I’m trying to avoid.

The only thing that I’ve thought of that would be a pretty poor solution (but it would probably work ok for my purposes) is just to have a table with very long list of numbers, 1-1000000 for example. Then, have the API workflow select a random number from that same list every time. It seems like it would work, but it seems like a kind of stupid solution. And would that load all the random number things in to the page order to pick a random one?

Any thoughts on how to do this? This should be really simple, but it is not!

It sounds like you are generating the random numbers in the page workflow and then trying to pass them to the API.
Have you tried generating the random number in the API workflow? This will result in a new random number being generated for each item in the list that you pass to the API.

Here’s an example that might be close to what you are trying to do:

That’s the first thing I tried. Unfortunately this method stores the random number as a string, but I need it to be a number, because after it is stored in the database I need to do some calculations/manipulations with it.

Another thing I tried, which I really thought would work, was to create a list on the page of n random numbers, and pass that list into the API workflow. Then, use a counter within the API workflow to index through each number 1-n, adding 1 to the counter each time the workflow runs on an item in the list. When I tried this, I got a whole bunch of random results–sometimes there would be unique random numbers, sometimes they would be the same. This makes me think that the API workflow (when run on a list) doesn’t wait until the first iteration is complete before starting the second. In many cases it appears to be running the workflow on multiple items simultaneously. Is there any way to force it to go in series instead of parallel? If I set a long time delay between the start of each iteration maybe that would be a workaround, but I’d rather force the first iteration to finish first, to avoid any chance of a mistake.

Let’s go back to square 1. The reason I need the random number generator in the first place is as follows–I have a list of things (already created, since they will be created at different times). At one moment in time, I want to finalize this list and arrange in a random fashion, giving each item a number from 1 to n based on the random sort, and store that value in the database. So if there are 5 things on the list, there should be a 1, 2, 3, 4, and 5. My original concept was to assign a random number when the thing is created. Then, other things in this list can be created at any later time, and also given a random number. Then when the right button is pressed, all the random numbers can easily be compared and numbers 1-5 assigned accordingly.

Is there another simpler way to do this? I can randomize the list in a repeating group, but I cannot figure out a way to save the index to the database.

Do you really need to use the numbers at all or would it work to create a random list of the things and then get thing number 1, thing number 2,… from that new list?

No, I don’t really need the random numbers at all. What I need is to be able to take an existing group of things, and randomly populate a field in each one of them with a number from 1 to n.

I can create a repating list and sort randomly, but I cannot figure out how to save an integer to the database based on this sorting, using a single button or workflow. I can do it if I put a button on each row of a repeating group, because then it gives me access to that cell’s thing. But for that to work, the user will end up needing to click on 10 (or more) different buttons, to save the value in each thing. There has got to be a way to do this with one button.

Other people have had similar issues–I posted in the following discussion hoping for some clarification, but I haven’t gotten any yet:

This seems to do what you want, but might be a bit slow for really long lists.

Set a new list of things by sorting the original list randomly.

Schedule and API workflow on this new “randomly sorted” list of things.

In the API workflow, make changes to the thing: add the count of the randomly sorted list to the thing’s Field (numberRandom) as a number.

Remove that thing from the randomly sorted list of things.

This should iterate through the entire list of n things and assign a random number from 1 to n to each of them.

Thanks for the help. I had another workaround going, but this seems to be work a bit better.

The only problem this has (which was one of the biggest problems in my other workaround too) was that if the API workflow runs too fast on subsuquent things (and too slow internal to that workflow), it assigns the next thing’s random number before the first one is removed, so instead of 1, 2, 3, 4, 5, I’ll get something like 1, 2, 3, 5, 5. Setting an interval of a few seconds on the workflow (I had to set it to 5 seconds) worked in my testing. But what happens if the server response is slow? Then you get 5, 5, 5, 5, 5, or something like that. I still haven’t found a way to force one iteration to finish before the next one starts (or at least a certain portion of the iteration). It really makes indexing tough because it relies on certain server performance to work properly.

In any case this is better than my previous solution–my previous solution I had to wait 30 seconds between workflows to reliably get the same result.

Figured out an alternative using custom states that doesn’t use the API and seems to be a bit faster.

On your button click, set the randomly sorted list as a custom state on the page and set the count of that list as another custom state on the page.

Create a custom workflow that makes changes the last thing thing in the custom state “list”, removes that thing from the custom state “list” and then subtracts one from the custom state number.

Trigger this custom workflow using a “do every 5 seconds” event with a conditional statement “and” custom state number >0.