0

I'm trying to shuffle the rows in my current array in Processing. This array contains 24 rows and is a two-dimensional-array. Can someone tell me what I do wrong?

float[][] shuffleArray(float[][] myArray) {   
  float[][] newArray = myArray;   
  int random1 = round(random(0, 24));
  int random2 = round(random(0, 24));
  float[] buffer1 = newArray[random1];
  float[] buffer2 = newArray[random2];
  newArray[random2] = buffer1;
  newArray[random1] = buffer2;   
  return newArray; 
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Dave
  • 290
  • 2
  • 12
  • Would you mind to tell us your programming language? – school_guy Oct 24 '17 at 11:41
  • java in processing – Dave Oct 24 '17 at 12:02
  • 1
    @Dave The phrase "java in processing" is somewhat ambiguous. Processing *is* a programming language in its own right (albeit one which is based on Java and runs on the JVM) but it isn't Java. I *think* your question is about Processing instead of Java, in which case your mention of Java just muddies the question. – John Coleman Oct 24 '17 at 12:07
  • It also isn't clear what you are trying to do. Are you trying to return a new array or are you trying to modify the passed array? Your use of `return` suggests the former, but you seem to be flirting with code that will modify the passed array as a side effect. Also, you said `24` elements but also "multidimensional". A single number doesn't adequately nail it down. Is 24 = 12 x 2? 4 x 6? Please clarify. – John Coleman Oct 24 '17 at 12:15
  • i'm trying to modify the order of the past array into a new array. the current array is myArray[24][3]. – Dave Oct 24 '17 at 12:22
  • Are you trying to shuffle the 24 rows, treating each row (perhaps thought of as a point in 3-dimensional space) as a unit? Or -- are you trying to freely shuffle the 24x3 = 72 elements so that rows are broken up? – John Coleman Oct 24 '17 at 13:14
  • I'm trying to shuffle the 24 rows as a unit. sorry for the bad explanation, my English is not that good. – Dave Oct 24 '17 at 13:55
  • You need to [debug your program](http://happycoding.io/tutorials/processing/debugging) by tracing through the code with a piece of paper and a pencil, or the debugger that comes with the Processing editor, until you understand where your code's execution differs from what you expected. – Kevin Workman Oct 24 '17 at 17:53

1 Answers1

1

To fix your problem, you first have to debug your program to figure out exactly what your program does.

The best thing to do is to go through your code line by line to understand what's happening.

float[][] shuffleArray(float[][] myArray) {   
  float[][] newArray = myArray;   
  int random1 = round(random(0, 24));
  int random2 = round(random(0, 24));
  float[] buffer1 = newArray[random1];
  float[] buffer2 = newArray[random2];
  newArray[random2] = buffer1;
  newArray[random1] = buffer2;   
  return newArray; 
}
  • First, you point a newArray variable to the myArray variable. Note that this means that both variables point to the same array, so changes to one will affect the other.

  • Then you create two random numbers, but note that those random numbers might be the same.

  • Then you get the sub-array at those indexes, which again might be the same. Then you swap the sub-arrays.

  • Then you return newArray, but remember that this is pointing to the same array that myArray is. So you're just modifying the input array and then returning a reference to it.

So, what this function does is maybe swap two sub-arrays in your 2D array. This is not the same as shuffling the whole array. Picture taking a deck of cards and maybe swapping two of the cards. That's not the same thing as shuffling the whole deck.

To understand references, I recommend you read this and then this.

Now, to fix your problem, Google is your friend. Searching for "java shuffle array" will return a ton of results, including this and this. If you have problems in the future, please try to work through this process of debugging your program so you understand exactly what your code is doing before you ask a question. Good luck.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107