I am using Collections.shuffle(list);
to shuffle a list but I don't know how to unshuffle it?I was thinking of saving the list before shuffling and then shuffle it so that a backup is maintained and can re restored whenever required ,but this seems like a inefficient way of doing it and will take up time and memory ....if you know a more logical way of doing it,can you please elaborate it ??
by the way here is how my app looks :D
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6] Method #2 : Using random.shuffle () This is most recommended method to shuffle a list. Python in its random library provides this inbuilt function which in-place shuffles the list.
If you seed your random source, you can recreate your list of shuffled numbers just by running the same shuffle on the same list of 0 to n. You can then use this to unshuffled your list of shuffled tuples, as before. Edit: trincot's answer implements this idea, but with working sample code.
By looping over each list in the list of lists, we can then easily apply the random.shuffle () function to randomize each sublist’s elements. Let’s take a look at what this looks like:
Shuffling a sequence of numbers have always been an useful utility and the question that has appeared in many company placement interviews as well. Knowing more than one method to achieve this can always be a plus. Let’s discuss certain ways in which this can be achieved.
There's no such concept of unshuffling - after you've shuffled a deck of cards, how would you get back to the previous state, for example?
If your original collection is ordered in some definitive way, just sort it again. Otherwise (e.g. if it's been hand-ordered) you must take a copy before you shuffle.
In theory you could:
Random
and pass that into shuffle
ArrayList<Integer>
from 0 to size (exclusive)Random
created with the original seed... but that's an awful lot of work. Unless your collection is really too big to keep the extra copy (don't forget it's just a copy of references, not whole objects), I'd just clone the collection before shuffling.
First, Jon's answer should be used, because that's the simplest and cleanest. Anyway, it is possible to reverse the shuffling with a known random source, since Collections.shuffle()
specifies the used algorithm. Again, do not do this, but here's the process for anyone curious:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Unshuffle {
public static void main(String[] args) {
List<Integer> data = new ArrayList<Integer>();
for (int i = 0; i < 20; i++) {
data.add(i);
}
System.out.println(data);
Collections.shuffle(data, new Random(42));
System.out.println(data);
unshuffle(data, new Random(42));
System.out.println(data);
}
private static <T> void unshuffle(List<T> list, Random rnd) {
int[] seq = new int[list.size()];
for (int i = seq.length; i >= 1; i--) {
seq[i - 1] = rnd.nextInt(i);
}
for (int i = 0; i < seq.length; i++) {
Collections.swap(list, i, seq[i]);
}
}
}
And the output is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] [15, 16, 14, 13, 5, 11, 17, 18, 8, 2, 6, 7, 3, 1, 19, 4, 12, 0, 9, 10] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With