Hello all!
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
- 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);
Does anyone have any idea of what could possibly be going on there? I would really appreciate it very much.
If you don't understand anything, let me know :)
Thanks! :D
1