I have a recurring problem that when I'm controlling leds, I make several different functions for different fx and just want to trigger them randomly but having a hard time to formulate it in an intelligent way and end up hardcoding every function call.
ex: I have let's say 10 functions called anim1();, anim2();, anim3(); etc and would like to called them randomly in that fashion :
randNumber = random(1,11);
anim[randNumber]();
I know that it's not that type of brackets I need to use but I can't find the proper syntax and I'm wondering if it's possible to do that.. I'm sure there must be a way
carapace:
ex: I have let's say 10 functions called anim1();, anim2();, anim3(); etc and would like to called them randomly in that fashion :
If the parameter lists of all functions are the same (i.e. no parameters at all) you could use an array of function pointers to holf the function addresses and call the functions by index in the arry.
Or, use a switch/case statement to call the appropriate function. Put the switch/case in a function that takes the random number, so calling it is simple:
Building on Whandall's example, if your functions do not need to be called elsewhere (outside of the array), or are relatively short, an array of lambdas may suffice: