To solve our "long-list-of-available-drinks-problem", we need to pick a random drink from our list of non-alcoholic drinks and save it so we can request its recipe in a second HTTP request.
Before we can pick a random item, we need to know how many drinks are actually in the list. In Action Flows, this "list" is called an Array.
What is an Array?
Think of an Array as a container.
The Square Brackets [ ] are the walls of the container.
Everything inside those brackets are the Collections { } (i.e., the individual drinks).
👉 Take a look at the available functions again.. Anything that looks like it could help to count the items (or collections) inside an array?
Solution
The function you're looking for is the length() function which is part of array functions.
Now that you know which function to use to solve at least a part of your problem, the next question is: Where to save the length of the array?
To answer this, it is time for you to get acquainted with the unofficial Swiss Army knife of Action Flows: Variables!
Variables in Action Flows can serve a variety of purposes, and their usage varies from being an integral part of the Action Flow to just a temporary solution to understand the outputs of one or multiple other preceding modules and operations better.
In our case, we choose the former: the Set variable module will become an integral part of our flow. However, not to just store the length of our array…
🕵️ It is time again for a quest! You've received a multitude of hints so far on how to proceed, and we'd like to put it again into your hands to figure out the solution. As before, multiple hints build on each other, and which you can expand as you need them.
Good luck!
Hint #1
The first step is to add the variable module to your flow. As before, right-click the dotted lines in between the HTTP module and the email module and add a new module - in this case, the "Set variable" module, which is part of Tools.
Hint #2
How did you name the variable that should be set through the new module?
We called it “Random non-alcoholic drink array index”.
(Pretty long name, we know, but it's supposed to be a hint 🤷)
Hint #3
We don't just want to save the length of the array (the total count of drinks) to a variable. Instead, we want to generate a random number within that range.
Why? Because our next goal is to make a second HTTP request to retrieve the full recipe for just one single drink. That request requires a Drink ID as an input. To get that ID, we have to "reach into" the list and grab it from a randomly chosen array position (also known as an Index).
We need to combine the length function with the random function. This tells the flow: "Count how many drinks we have, and then pick a random 'seat number' between 1 and that total."
Why only save the Index? Think of the Index as a coordinate. By saving this random number to a variable, we create a steady "pointer" that we can use in the next step to reach into our array and say: "Give me the ID of the drink sitting at exactly this position."
We know this sounds a bit complicated, but you're nearly there!
Hint #4
Always make sure to hover over the functions in the Action Flow editor to read the info on how exactly the respective function works and what parameters it requires.
random, for example, generates a floating-point number between 0 and 1, e.g. 0.45.
How would you need to combine it with the array's length to obtain a random number that's between 0 and the array's length?
Hint #5
You need to multiply random by the length() function to obtain a random index. Since random generates floating-point numbers, you need to wrap your entire construct into another round() function. Otherwise, the next step, to obtain the idDrink value at a specific array index, will not work.
Here is the (almost) final version of what your variable value should look like:
round( random * length( data: drinks[] ) )
There is a tiny but vital detail to remember: in Action Flows, Array indices start at 1 rather than 0. If our random generator picks 0.000, the calculation becomes 0.000 * 58, which equals 0. Since there is no "Seat 0" in our array, the module would return an empty response. To prevent this, we might be tempted to simply add +1 to our formula—but that creates a new problem at the other end of the spectrum.
Imagine the random number is 0.999. Multiplied by 58, it returns 57.942. The round() function would push this up to 58, and adding our +1 would suddenly result in an index of 59—another non-existent position! So, how do we keep our "pointer" perfectly within the lines?
Mastering Floor and Ceil
The secret lies in swapping out round() for a more precise pair of functions: floor() and ceil().
While the floor function "drops" a decimal down to the nearest whole integer (like hitting the ground), the ceil function (short for ceiling) always "pushes" it up to the next whole integer.
By using floor(random * length) + 1, we ensure that even a 0.999 result only ever reaches 57, which the +1 then corrects to a perfect 58. Our logic is now officially "bulletproof."
Your final result looks like this:
Make sure you've mapped the correct array item from the HTTP module inside the length function.
Did you get it right?
Our Set Variable module now outputs a randomly chosen index of the drinks array.
The next step is to retrieve the recipe for our chosen drink. Bottoms up!