# Help generating random permutations from array list

Printable View

• December 2nd, 2012, 06:19 PM
mactank13
Help generating random permutations from array list
There is no output for this code. I don't know why. It seems like it should work. Can someone please let me know what they think i need to change. Here is the problem.

https://students.ga.desire2learn.com...eeResponse.pdf

Code Java:

import java.util.Random; //imports the Random java class
public class PermutationGenerator

{
/*This method iterates the nextPermutation() method 10 times
*to produce 10 differernt arrays of size 10 with integers
*1 to 10 inclusive*/

public void PGenerator()
{

PermutationGenerator a = new PermutationGenerator();
for (int i=0;i<10;i++) a.nextPermutation();//This loop repeats the nextPermutation method 10 times to produce 10 different arrays
}

/*This method returns an array of 10 integers from
*1 to 10 inclusive in a random order by creating a
*new array from permutations of a base array*/
public int[] nextPermutation()
{
Random rand = new Random();//Creates the random generator
int baseSize = 0;//Initializes the base counter as 0
int[] base = new int[10];//Initializes the base array
for (int i = 0; i<10; i++){ //This loop fills up the base array with integers 1 to 10 inclusive
base[i] = i + 1;
baseSize++;//Increases the counter for base size
System.out.println (base);
}
int[] mutation = new int[10];//Initializes the mutation array
for (int i = 0; i<10; i++){
int a = rand.nextInt(baseSize); //Picks a random index between 0 and the length of the base array
mutation[i] = base[a]; //Sets the ith index of the result mutation array to the value at a in the base
for (int j = a; j<baseSize - 1; j++){
base[j] = base[j + 1];//This loop deletes the value at index a in the base array to prevent it from being chosen again
System.out.println (base);
}
baseSize--;//Decreases the length of the base array
}
return mutation;
}

public static void main(String[] args)
{
}
}

public static void main(String[] args)
{
}

• December 2nd, 2012, 06:40 PM
Norm
Re: Help generating random permutations from array list
Quote:

There is no output for this code
Add some println statements that print out messages as the code is executed and shows the execution flow and the values of variables as they are changed and used.
Put the println statements in all the methods so you know what is being executed.
• December 2nd, 2012, 06:43 PM
mactank13
Re: Help generating random permutations from array list
i just tried that and not even the first part is being executed.
• December 2nd, 2012, 06:57 PM
Norm
Re: Help generating random permutations from array list
What methods did you see were being executed? What was printed out?
Did the method that was executed call any other methods?
Where those other methods executed?
You'll know when a method is executed when the println statement prints out a message.
• December 2nd, 2012, 07:01 PM
mactank13
Re: Help generating random permutations from array list
Nothing is executed. It still doesn't print anything for any step.
• December 2nd, 2012, 07:09 PM
jps
Re: Help generating random permutations from array list
So take Norm's advice, and locate the first method to be called, and see what it does. Surely at least one method gets called somewhere, sometime, right? Start there.
• December 2nd, 2012, 07:09 PM
Norm
Re: Help generating random permutations from array list
Are there println methods in all the methods? The one in the main() method should print.
If the main() method doesn't execute, then you must get an error message.
• December 2nd, 2012, 07:26 PM
mactank13
Re: Help generating random permutations from array list
I placed a println method at each step and nothing is printed. There is no error message.
• December 2nd, 2012, 07:27 PM
Norm
Re: Help generating random permutations from array list
The one in main() has to print. Can you post the new code that shows where the printlns are located?
• December 2nd, 2012, 07:41 PM
mactank13
Re: Help generating random permutations from array list
I edited the original post to reflect changes.
• December 2nd, 2012, 07:44 PM
Norm
Re: Help generating random permutations from array list
Where are the println statements?
Please post the code with the println statements that you were executing that did not print out anything when it was executed.
• December 2nd, 2012, 08:01 PM
mactank13
Re: Help generating random permutations from array list
they are in the code. maybe i did not put them in the right spots? there are two.
• December 2nd, 2012, 08:04 PM
Norm
Re: Help generating random permutations from array list
Quote:

there are two.
There should be one in each method at the first thing in the method. You want to see where the code is executing.
There should be lots of them. 2 is not enough.