# Calling random integers/arrays

Hi everyone, and thanks in advance for the help.

If you had a group of ten integers or arrays, all named the same except for a number at the end, is there a way you could randomly call one of them based on that number?

Obviously this code doesn't work, but hopefully it gives you an idea of what I'm thinking. Thank you!

for (int RowShooter = 14; RowShooter >-1; RowShooter--) {strip.setPixelColor(RowPlus**(Rando)**[RowShooter],80,0,0);}

Right, so in theory this would run a random array named RowPlusX, with X being the suffix number of an array within the random scope.

Hopefully it makes sense what I'm trying to do. Thanks!

cbowen:
Hi everyone, and thanks in advance for the help.

If you had a group of ten integers or arrays, all named the same except for a number at the end, is there a way you could randomly call one of them based on that number?

Obviously this code doesn't work, but hopefully it gives you an idea of what I'm thinking. Thank you!

for (int RowShooter = 14; RowShooter >-1; RowShooter--) {strip.setPixelColor(RowPlus**(Rando)**[RowShooter],80,0,0);}

Right, so in theory this would run a random array named RowPlusX, with X being the suffix number of an array within the random scope.

Hopefully it makes sense what I'm trying to do. Thanks!

Sure, it makes sense. And you have already encountered it. ARRAYS. Make an array of 10 integers. To access, they are numbered 0 through 9. Let random pick a number between 0 and 9 and there you go!

Paul

Thanks Paul. I understand WHAT you're saying, just not how to apply it in this case. I may need a sample line of code if it's not too much trouble. Thanks again.

``````int randomNumbers[] = {10,56,74,22,63,522,-1};
``````

To access a member of that array (for example the first one, 10) use

``````randomNumbers[0];
``````

Remember that arrays start counting at 0.

If you want to call a random number from that array, just replace the 0 with a randomly generated number. Make sure not to use a number bigger than one less than the size of your array (since counting starts at 0). So in this example calling `randomNumbers[7];` would give unexpected results.

See here for the arduino reference on generating random numbers.

Right, I don’t actually want to call a random number FROM an array. I want to call a random array from a group of them.

So imagine if you had a grid of LED’s, and you’ve made an array for each row that maps the LED numbers in that row. Then, you want to call random arrays (rows) to light up.

I want to call a random array

You call functions. You do not call arrays.

You reference arrays. A 2D array is an array of arrays. You can randomly chose which array in the array, but using a random number for the first index.

It sounds like you want a 2D array. See here C++ Multi-dimensional Arrays to get you started.

Thanks everyone. It took a couple hours or internet research to wrap my head around 2d arrays, but I made it work last night. The effect ended up sucking, but at least I learned something new. Thanks again.

That’s a problem with display hacks. it is very hard to predict what’s gonna suck and what’s gonna catch peoples’ eyes.

But always learn from mistakes. Sometimes it turns out later you want to do something like what once sucked… and besides, you did learn a new trick even if it weren’t the answer.

Sometimes a program error will make an absolutely unexpected and pretty display. I’ve changed my mind about where I was headed on the basis of such results.

a7