in blue j Write a method that will produce random permutations of the numbers 1
ID: 3768636 • Letter: I
Question
in blue j
Write a method that will produce random permutations of the numbers 1 to 10. To generate a random permutation, you need to fill an array with the numbers 1 to 10 so that no two entries of the array have the same contents. You could do it by brute force, by calling Math.random() until it produces a value that is not yet in the array. That may take some time, and wouldn’t be efficient. Instead you should implement a smart process; Make a second array and fill it with the numbers 1 to 10, then pick one of those at random, remove it from the second array and append it to the permutation array. Repeat this 10 times.
Create a main method that will test this method along with a printArray(int [] arr) method to print it out. Print out 5 or 6 arrays in your main method created by your randomizing method to ensure it is working appropriately.
Explanation / Answer
Explaination:
1. Create a class PermutationGeneratorTest in the project, and type the source code that contains the main in it. Your main method should read a number n, create an object of PermutationGenerator , call the method nextPermutation() (as below) to produce a permutation of numbers 1 to n, and print out the permutation.
2. Create a class PermutationGenerator in the project, and type the source code. Without using the constant 10, your program should produce random permutations of numbers 1 to n. The constructor accepts the parameter n, and the method int[] nextPermutation() produces the random permutations.
3. Compile all source code and run the byte code.
Program:
import java.util.Random;
/**
* Permutate numbers between 0 and n
*/
class PermutationGenerator {
/**
* Field
*/
private int permut[];
/**
* Constructor
* @param int the number of numbers in the permutations
*/
public PermutationGenerator(int n) {
permut = new int[n];
}
/**
* Generate a permutation
* @return int[] the permutation of numbers 1 to n
*/
public int[] nextPermutation() {
// Create a random generator
Random generator = new Random();
int n = permut.length;
for (int i=0; i<n; i++) {
while (true) {
// Generate a random number between 1 and n
int nextRandomNumber = generator.nextInt(n)+1;
boolean next = true;
for (int j=0; j<i; j++)
if (permut[j]==nextRandomNumber) {
next = false;
break;
}
if (next) {
permut[i] = nextRandomNumber;
break;
}
}
}
return permut;
}
}
/**
* PermutationGenerator test
*/
public class PermutationGeneratorTest {
public final static void main(String[] args) {
int n = Integer.parseInt(args[0]);
PermutationGenerator generator =
new PermutationGenerator(n);
int[] p = new int[n];
for (int i=0; i<n; i++)
{
System.out.println("The permutation " + (i+1) + " of numbers between 1 and " + n +": ");
p = generator.nextPermutation();
for (int i1=0; i1<n; i1++)
System.out.print(p[i1] + ", ");
System.out.println("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.