Ansen
March 18, 2014, 11:28pm
#1
Is there a way to reference a random array from a list of arrays?
Here's what I'd like to be able to do. (I know this doesn't work). I have 42 different arrays and I want to choose one at random.
The array names are array1, array2, array3, etc.
DmxSimple.write(p + 5, array(random(42)) );
system
March 18, 2014, 11:32pm
#2
Create an array of pointers to your arrays, and then an index into that array "chooses" one.
system
March 19, 2014, 3:06am
#3
Or create a two dimensional array, and use your random value as the first index.
AWOL:
Create an array of pointers to your arrays, and then an index into that array "chooses" one.
Can you give me an example, please?
Greetz Chris
system
April 24, 2014, 9:56am
#5
Uncompiled, untested
char*strings [] = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "."};
const int N_STRINGS = sizeof (strings) / sizeof (strings[0]);
void setup ()
{
Serial.begin (115200);
for (int i = 0; i < 20; i++)
{
Serial.println (strings [random (0, N_STRINGS)]);
}
}
void loop ()
{}
PeterH:
Or create a two dimensional array, and use your random value as the first index.
In this case would it be a problem, if the lines have different amount of indexes?
Greetz Chris
Edit: Sorry for posting two times..
system
April 24, 2014, 10:19am
#8
In this case would it be a problem, if the lines have different length?
The example in reply #4 doesn't care about size, because the elements are string pointers, but if you need to know the the size of the array you've chosen, and null-termination isn't feasible then you'll need another array (or a struct) to record the size of the array as well.