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)
{
}
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.
Re: Help generating random permutations from array list
i just tried that and not even the first part is being executed.
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.
Re: Help generating random permutations from array list
Nothing is executed. It still doesn't print anything for any step.
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.
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.
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.
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?
Re: Help generating random permutations from array list
I edited the original post to reflect changes.
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.
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.
Re: Help generating random permutations from array list
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.