I'm creating a function that generates an array with randomized positions. I made a code that works perfectly when I run it by itself, but when I put it inside the function, it suddenly crashes and all I get is an infinite loop.
Here's how I call the function:
String[] arrShuffled = createShuffledArray(6); // Using number 6 as example
The output for println(arrShuffled) should be something like this:
[0] = "5"
[1] = "3"
[2] = "1"
[3] = "0"
[4] = "2"
[5] = "4"
Now this is the function code:
String[] createShuffledArray(int intTotal){
String[] arrReturn = new String[intTotal]; // This will be the variable i'm going to return at the end of the function
// This string saves all numbers that were sorted before, so I can check in here if a number has been sorted and then sort again until it mismatch.
// This way I'll get NO repeated numbers
String strHistory = "";
// Do the loop from 0 to the total of numbers i want to get randomized
for(int i=0;i<intTotal;i++){
int id;
do{
id = (int) random(0, intTotal);
//println(match(strHistory, "["+id+"]")); // Used as test
}while(match(strHistory, "["+id+"]") != null);
// Adds the sorted number to the history
strHistory += "["+id+"]";
// Saves the randomized number at position "i" in arrReturn
arrReturn[i] = id+"";
} // End FOR
return arrReturn;
} // End Function
When I try to run this function, all I get is an infinite loop ending nowhere. Otherwise, if I remove the first and last lines of the above code (turn the function in simple code), it works perfectly. Try running this code below in your processing and you'll see:
int intTotal = 6;
String[] arrReturn = new String[intTotal];
String strHistory = "";
for(int i=0;i<intTotal;i++){
int id;
do{
id = (int) random(0, intTotalPares);
//println(match(strHistory, "["+id+"]")); // Used as test
}while(match(strHistory, "["+id+"]") != null);
strHistory += "["+id+"]";
arrReturn[i] = id+"";
}
println(arrReturn);
I have already tried every possible way to make it work, but it didn't happen. Looks like an weird problem when setting this routine as a function. I wonder if it's not some Black Magic based on processing core or how it "reads" and "understands" our codes.
Does anyone have any idea of what could possibly be going on there? I would really appreciate it very much.