I have a list of integers (currently using cern.colt.list.IntArrayList). I can call "shuffle()" and randomly shuffle them. I would like to be able to reproduce a shuffle. I can reproduce a series of random numbers by setting a seed. I do not seem to be able to set a seed in this case. What should I do? I am open to other implementations.
We can create a list from the array and then use the Collections class shuffle() method to shuffle its elements. Then convert the list to the original array.
In order to shuffle elements of ArrayList with Java Collections, we use the Collections. shuffle() method. The java. util.
Set is unordered, so randomizing an unordered Collection doesn't make any logical sense. An ordered Set is ordered using a Comparator which means it has a fixed order, you can't shuffle it, that has no meaning as the order is determined by the Comparator or the compare() method.
This is possible by using the shuffle method that allows you to provide the backing Random
instance: Collections.shuffle(List<?> list, Random rnd)
:
Example:
Collections.shuffle(yourList, new Random(somePredefinedSeed));
You can specify the Random instance with a seed value using public static void shuffle(List list, Random rnd). For the Random(long seed) constructor you can specify a seed.
From Java Docs:
Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair.
there is an alternative method which takes a Random as source
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