Java Utililty Methods Array Shuffle

List of utility methods to do Array Shuffle

Description

The list of methods to do Array Shuffle are organized into topic(s).

Method

Listshuffled(List list, Random random)
shuffled
Collections.shuffle(list, random);
return list;
ListshuffledList(List list)

shuffledList

ArrayList<T> result = new ArrayList<T>(list);
Collections.shuffle(result);
return result;
voidshuffleInPlace(E[] elems, Random rand)
shuffle In Place
for (int j = elems.length - 1; j > 0; j--) {
    int randIndex = rand.nextInt(j + 1);
    E tmp = elems[randIndex];
    elems[randIndex] = elems[j];
    elems[j] = tmp;
voidshuffleInPlace(int[] toShuffle, Random random)
Randomly shuffle the specified integer array using a Fisher-Yates shuffle algorithm
for (int i = 0; i < toShuffle.length - 1; i++) {
    int j = i + random.nextInt(toShuffle.length - i);
    int temp = toShuffle[i];
    toShuffle[i] = toShuffle[j];
    toShuffle[j] = temp;
voidshuffleIntArray(int[] arr)
shuffle Int Array
if (arr == null || arr.length == 0)
    throw new IllegalArgumentException("Array cannot be null or have zero length");
for (int i = arr.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = arr[index];
    arr[index] = arr[i];
    arr[i] = a;
int[]shuffleIntArray(int[] array)
shuffle Int Array
int[] copy = array.clone();
Random rnd = new Random();
for (int i = copy.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = copy[index];
    copy[index] = copy[i];
    copy[i] = a;
return copy;
voidshuffleList(List list)
Shuffles a list.
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));
voidshuffleList(List list)
Shuffles the input list using an implementation of the Knuth "Algorithm P" shuffle.
Random randomGenerator = new Random();
for (int loopIndex = list.size() - 1; loopIndex > 0; loopIndex--) {
    int randomIndex = randomGenerator.nextInt(loopIndex + 1);
    Collections.swap(list, randomIndex, loopIndex);
voidshuffleSublist(List objects, int start, int end)
Shuffle the elements between the specified start and end index both inclusive.
for (int index = start; index <= end; index++) {
    T current = objects.get(index);
    int randomIndex = nextInt(start, end);
    T randomObject = objects.get(randomIndex);
    objects.set(index, randomObject);
    objects.set(randomIndex, current);