Loading...
Logo
Processing Forum

Hi,

 

I am parsing floating point numbers from 5 .csv files through Processing and ultimately end up with 5 nos. arrays that contain the parsed data for each file. Each array consists of 199 elements. So far so good.

 

Now, what I need to do is to get Processing to grab one of the arrays, at random, so it knows ‘ok, use item3Array’, together with its associated contents list. I was thinking to list the arrays as a 1D array and to call a random index from the 1D array:

 

float[] myOneDimensionalArrayofArrays = { item1Array,  item2Array,  item3Array, item4Array, item5Array  };

 int randomChoice = myOneDimensionalArrayofArrays  [(int)(random(5))];

 

However, the syntax doesn’t work in the manner I tried – I guess I need to find another way of listing the ‘array of arrays’. I looked at a number of proposals posted on the Forum previously. Concatenating the arrays or creating an ArrayList, as I understand it, just creates a long list of data, rather than a list from which I can grab a subgroup from, unless I somehow looped through every 199th element to get the randomly chosen subgroup.

Might anyone have an idea how I could approach this matter?

Replies(2)

Well...

If you concatenate all of your arrays, the process would look something like this:
Have five arrays
Concatenate them
Select a random array from 0 to 5 (say that value is called randomArray)
Now select a random number from the array, 0 to 199 ( randomNumber)
So the final value is equal to randomNumber + (randomArray * 200)

Now if you followed that, you should be able to do what you want.

As for the ArrayList, it might help if you look here.
An ArrayList is a "resizable array implementation of the List interface".
In other words, you can store a List of Objects in it and delete some, add some more, etc.
For your purposes, you don't really need that much functionality.

You could potentially create an Object that stores an array and place five instances of that Object within the array, but that is probably a little overkill...

There are many ways that you could do what you want to achieve.
You need a 2D array. In Java a 2D array is constracted as a 1D array of 1D arrays to do that with your arrays then use this syntax

Copy code
  1. // Create the 2D array from the 1D arrays
  2. float[][] myOneDimensionalArrayofArrays = new float { item1Array,  item2Array,  item3Array, item4Array, item5Array };

    // Get an array at random i.e. 0 - 5)
    int randomArrayNo = (int) random(5);

  3. // Get a random value from the selected array
  4. int randomChoice = myOneDimensionalArrayofArrays  [randomArrayNo ][(int) random(199)];