Shuffle Compound Shuffled List

I am trying an un-shuffle compound shuffled list. It’s hard for me to figure out how to do this.

public static void main(String[] args) {
    // Setup random
    Random rand = new Random();
    rand.setSeed(5);
    // Setup list
    ArrayList<Character> list = new ArrayList<Character>(Arrays.asList('v','y','2','w','9','n','8','v','a'));
    // Compound shuffle list
    for(int i=0;i<5;i++)
        Collections.shuffle(list, rand);
    // un-shuffle list
    // TODO
}

And also my un-shuffle method.

private static void unshuffle(ArrayList<?> list, Random rand) {
    // Create the sequence backwards
    int[] seq = new int[list.size()];
    for(int i=seq.length; i>1; i--)
        seq[i-1] = rand.nextInt(i);
    // Traverse the sequence and swapping it inversely
    for (int i=0; i<seq.length; i++)
        Collections.swap(list, i, seq[i]);
}

Edit: fixed ArrayList.

+4
source share
3 answers
public class Main {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>(Arrays.asList("A", "B", "C", "D", "E", "F", "G"));
        compoundShuffle(list, 8, 13);
        compoundUnshuffle(list, 8, 13);
        System.out.println(list);
    }

    public static void compoundShuffle(List<?> list, int repetition, long seed) {
        Random rand = new Random(seed);
        for (int i = 0; i < repetition; i++)
            Collections.shuffle(list, rand);
    }

    public static void compoundUnshuffle(List<?> list, int repetition, long seed) {
        helper(list, repetition, seed);
    }

    private static <E> void helper(List<E> list, int repetition, long seed) {
        List<Integer> indices = new ArrayList<Integer>();
        int size = list.size();
        for (int i = 0; i < size; i++)
            indices.add(i);
        compoundShuffle(indices, repetition, seed);
        List<E> copy = new ArrayList<E>(list);
        for (int i = 0; i < size; i++)
            list.set(indices.get(i), copy.get(i));
    }
}
+3
source

Two things you need to know:

If two instances of Random are created with the same seed, and the same sequence of method calls is performed for each of them, they will generate and return identical sequences of numbers.

  1. Behavior of the # shuffle () method of Collections # methods. This paragraph is key:

, , " ". , , .

, , .

+2

, - , .

seed ( ).

, Fisher-Yates (, , Collections.shuffle), . int [], Object [].

// implementing Fisher–Yates shuffling using a seed
public static void shuffleArray(int[] ar, int seed) {
    Random r = new Random(seed);
    for (int i = ar.length - 1; i > 0; i--) {
        int index = r.nextInt(i + 1);
        // simple swap
        int a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}   

// implementing Fisher–Yates deShuffler
//(you should know the **seed** used for shuffling - this is the decryption key)
public static void deShuffleArray(int[] ar, int seed) {
    //rebuild your random number sequence
    Random r = new Random(seed);
    int[] randoms = new int[ar.length-1];
    int j = 0;
    for (int i = ar.length - 1; i > 0; i--) {
        randoms[j++] = r.nextInt(i + 1);
    }

    //deShuffling
    for (int i = 1; i < ar.length; i++) {
        //use the random values backwards
        int index = randoms[ar.length - i - 1];
        // simple swap
        int a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}
0

Source: https://habr.com/ru/post/1569920/


All Articles