Randomize the function Called

I wrote 50 different functions and I want one of those functions be randomly chosen to run every time a switch it hit, but I’m unsure how to approach this. I was thinking of using the random(min,max) command, something like:

function_0

function_1

function_2

….etc

If (switch == HIGH)
{
x = random(0,49);
function_x
}

Is there a better way to accomplish this or does this seem like a good approach?

Search "pointers to function". Try my Right-Left Rule. Then try this program:

void function1();     // Function prototypes
void function2();
void function3();
                      // An array of pointers to function
void (*functionPtr[])() = { function1, function2, function3};


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  randomSeed(A0);       // Seed random number generator
}


void loop() {
  long rnd = random(0, 3);
  functionPtr[rnd]();       // Call a function in random sequence
}

void function1()        // The dummy functions
{
  Serial.println("In function1()");
}
void function2()
{
  Serial.println("In function2()");
}
void function3()
{
  Serial.println("In function3()");
}

Because the name of a function resolves to a memory address, we can save a pointer to that address. All we did here was make an array of those pointers and use it to transfer program control to the function that is pointed to. The random number generator simply picks those pointers at random and then calls the appropriate function.

EDIT: Dam, econJack beat me to it.

My example has return and a single param though. Multiple params can be used if needed.

You could use an array of functions, then use rand to point to array index. Like this

String function0 (int param)
{
   char returnString[100];
   sprintf(returnString,"Random Number: %d ==> function0",param);
   return returnString;
}
String function1 (int param)
{
   char returnString[100];
   sprintf(returnString,"Random Number: %d ==> function1",param);
   return returnString;}

String function2 (int param)
{
   char returnString[100];
   sprintf(returnString,"Random Number: %d ==> function2",param);
   return returnString;
   }


// an array of funtion pointers initialised with our functions.
String (*arrayOfFunctionPointers[3]) (int param) = {function0,function1,function2};



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:


  int randomNumber = random(3); //random number from 0 to 2;

  //call the function in the array
  String result = (*arrayOfFunctionPointers[randomNumber])(randomNumber); 

  //print the result.
  Serial.println(result);

  delay(1000);

}

You can make use of an array of function pointers.

void (*myfunctions[])() =
{
  func1,
  func2,
};

where func1 and func2 are just two of your functions.

The functions need to be known to the compiler before you can used them in the array. You can either use prototypes like below

void func1();
void func2();

void (*myfunctions[])() =
{
  func1,
  func2,
};

void setup()
{
}

void loop()
{
}

void func1()
{
  Serial.println("func1");
}

void func2()
{
  Serial.println("func2");
}

Or you can define the functions before the array.

void func1()
{
  Serial.println("func1");
}

void func2()
{
  Serial.println("func2");
}

void (*myfunctions[])() =
{
  func1,
  func2,
};

void setup()
{
  Serial.begin(115200);
}

void loop()
{
}

And next you can implement the random calling of your functions in loop();

void loop()
{
  // get a random number as index into the array; limited to the number of elements of the array
  int i = random(0, sizeof(myfunctions) / sizeof(myfunctions[0]));
  Serial.println(i);

  // if a function was specified
  if (myfunctions[i] != NULL)
  {
    // call it
    myfunctions[i]();
  }
  delay(1000);
}

The test for NULL is a safeguard.

If your functions take parameters or return values, the approach stays the same but the code needs some modifications (see Pearo's reply).

Ah, I see I'm too late as well.

Awesome, Thank you very much, I'll give it a shot!

One last question:

Assuming power will be turned on/off to the Arduino (nano), is there an easy way to have the Arduino run a specific predetermined sequence the first time power is turned on, and then make the function calls random anytime the power turned on after the first time? Basically I would have have a bunch of "if" statements that check some type on non-volatile memory to see if power has been turned on? Does something like that exist? Thanks again.

You can do a predefined sequence in setup(). Do you need to differentiate between power cycle and reset?

I think so. If possible I want the functions to run in a specific order only once (the first time its ever powered up), and then to run randomly forever after that, even if power is lost. It doesn't actually have to be forever, but definitely for a number of days or weeks.

In short, I'm making a gift using the Arduino and it would be neat to make it run a certain way the first time its ever used, but after that things would get to repetitive, predictable, and boring...so I wanted functions to be called randomly. Definitely not needed, just something I was curious about.

Use a flag stored in EEPROM. You test the flag in setup. If not set, you can do the predefined sequence. This predefined sequence can e.g. be stored in EEPROM or be hardcoded.

After that initial sequence (and as part of it), you can set a flag in EEPROM to indicate that initial run has been done.

This will survive power cycles and hard resets when connecting the serial port or pressing the reset button.