comparison src/main/gov/nasa/jpf/util/RandomPermutationGenerator.java @ 5:1ba6ea44e5f9

slight fix of RandomPermutationGenerator, which should re-shuffle the original array, not the previous permutation
author Peter Mehlitz <pcmehlitz@gmail.com>
date Thu, 05 Feb 2015 19:13:42 -0800
parents d0a0ff1c0e10
children 3a19eedcc13d
comparison
equal deleted inserted replaced
4:d0a0ff1c0e10 5:1ba6ea44e5f9
31 public class RandomPermutationGenerator extends PermutationGenerator { 31 public class RandomPermutationGenerator extends PermutationGenerator {
32 32
33 protected int seed; 33 protected int seed;
34 protected Random rand; 34 protected Random rand;
35 35
36 protected int[] orig;
37
36 public RandomPermutationGenerator (int nElements, int nPermutations, int seed){ 38 public RandomPermutationGenerator (int nElements, int nPermutations, int seed){
37 super(nElements); 39 super(nElements);
38 this.nPermutations = nPermutations; 40 this.nPermutations = nPermutations;
39 rand = new Random(seed); 41 rand = new Random(seed);
42 orig = permutation.clone();
40 } 43 }
41 44
42 @Override 45 @Override
43 protected long computeNumberOfPermutations() { 46 protected long computeNumberOfPermutations() {
44 return nPermutations; // it's input (set) 47 return nPermutations; // it's input (set)
56 if (nGenerated == 0){ 59 if (nGenerated == 0){
57 nGenerated = 1; 60 nGenerated = 1;
58 return permutation; 61 return permutation;
59 62
60 } else if (nGenerated < nPermutations){ 63 } else if (nGenerated < nPermutations){
61 int[] perm = permutation.clone(); 64 permutation = orig.clone();
62 for (int i=0; i<nElements; i++){ 65 for (int i=0; i<nElements; i++){
63 int r = i + rand.nextInt( nElements-i); // i <= r < nElements-1 66 int r = i + rand.nextInt( nElements-i); // i <= r < nElements-1
64 swap(perm, r, i); 67 swap(permutation, r, i);
65 } 68 }
66 nGenerated++; 69 nGenerated++;
67 permutation = perm;
68 return permutation; 70 return permutation;
69 71
70 } else { 72 } else {
71 throw new NoSuchElementException(); 73 throw new NoSuchElementException();
72 } 74 }