There are times in a flow when I just want the next node to choose something at random. Other tools like NodeRed have this and its very useful.
I created a code node to achieve this, but would be nicer and more stable to have a simple “Random” node.
const items = [];
const myList = $input.all();
const randomIndex = Math.floor(Math.random() * myList.length);
const randomItem = myList[randomIndex];
return [{ json: randomItem }];
Jon
March 14, 2023, 8:47am
2
Hey @RedPacketSec ,
What sort of random data would you be interested in? We have a “debug helper” node that was released as a community node that can generate some random data.
Let us know what is missing and we can always look at popping in some changes to make it more useful.
1 Like
Thanks for that, but i need to choose an item from a list at random from the previous node.
Jon
March 14, 2023, 8:52am
4
Ah ok that is a bit different, We could add it as an option to return a random input item though
What does the output randomiser look like in other tools maybe we can get some inspiration.
FelixL
March 16, 2023, 4:52pm
6
@RedPacketSec you can do pretty much the same in n8n like in node red:
If you only want less outputs, just change the expression accordingly:
1 output: {{ Math.floor(Math.random() * 1) }}
2 output: {{ Math.floor(Math.random() * 2) }}
3 output: {{ Math.floor(Math.random() * 3) }}
4 output: {{ Math.floor(Math.random() * 4) }}
EDIT: removed set node as you can just put this expression directly into the switch node
You can choose a random item using the ‘item lists’ node: first sort the items randomly then limit to only return one: