Array processing function

A change Chrisir made but didn’t mention (and that I realized after going to bed last night) is that int(random(100)) returns values from 0 up to 99 which might occasionally give a value of 0 and you can’t compute mod 0. So it’s better to limit the range to random(1, 100).

Even better, there is no reason to use % at all. Just count down each frame and leave frameCount out of it.

if( --amt == 0 ) {
  arr = coord();
  amt = int(random(1, 100));
}

But that’s all a side issue to your variable scoping.

Any variable that you declare inside of draw() will get re-declared every frame. If you want anything to persist for more than 1/60 of a second, you have to scope the variable globally. You can call coord() repeatedly and get different sets of randomized arrays, but if you want the results to last, you have to use multiple arrays that are all stored at the global scope – either different variables or an array of arrays.

You can also think of an array of arrays as just one big 1-dimensional array. Fill an array with 60 numbers and just use 20 at a time.

arr = new float[60];  // at the global scope

for( int n=0; n<3; n++ )
  for( int i=0; i<20; i++ )
    circle( arr[ n*20+i ], height*(n+0.5)/3, 20 );
1 Like