Shuffle with random node

Hi, I need to randomize a playlist, I can't find a module that generates me a random series of numbers from 0 to 20, for example, so need to generate a random number, check if it was already generated, if not store it into an array.
Do you have an idea how to create it without overloading too much the flow with messages? (the latest number for examlple, will be matched after N attempts, and this I would avoid. Thank you!

This change node will select a random number from the initialised array, it will not select from the first five elements, the selected number is moved to the beginning of the saved array and can not be selected again for 5 selections, you must initialise the array first.

[{"id":"4187d2b2ef2b0e3e","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":100,"wires":[["95b966131f05f101"]]},{"id":"95b966131f05f101","type":"change","z":"d1395164b4eec73e","name":"slect a random number","rules":[{"t":"set","p":"random_array","pt":"msg","to":"$shuffle($flowContext(\"random_array\")[[5..19]])","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"random_array[14]","tot":"msg"},{"t":"set","p":"random_array","pt":"flow","to":"$append($append([$$.payload], $flowContext(\"random_array\")[[0..4]]), $$.random_array)[[0..19]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":100,"wires":[["8036687d187329ba","ac6b286c71a01f58"]]},{"id":"8036687d187329ba","type":"debug","z":"d1395164b4eec73e","name":"debug 2561","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":140,"wires":[]},{"id":"d0da769320e30ab5","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":40,"wires":[["a1b5c62edcf5d3e4"]]},{"id":"a1b5c62edcf5d3e4","type":"change","z":"d1395164b4eec73e","name":"initialise array","rules":[{"t":"set","p":"random_array","pt":"flow","to":"$shuffle([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":40,"wires":[[]]},{"id":"ac6b286c71a01f58","type":"debug","z":"d1395164b4eec73e","name":"debug 2562","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"$flowContext(\"random_array\")","targetType":"jsonata","statusVal":"$flowContext(\"random_array\")","statusType":"jsonata","x":610,"y":200,"wires":[]}]

[edit] The initialised array can be an array holding any type numbers, strings or objects etc.

1 Like

Give this a try, each time you inject the list is randomised.