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

int[]shuffle(int[] arr)
shuffle
if (arr == null) {
    return new int[0];
return random(arr, arr.length);
int[]shuffle(int[] arr)
shuffle
Random r = new Random();
int[] res = arr.clone();
for (int i = arr.length; i > 1; i--) {
    int i1 = i - 1;
    int i2 = r.nextInt(i);
    int t = res[i1];
    res[i1] = res[i2];
    res[i2] = t;
...
int[]shuffle(int[] array)
shuffle
for (int i = 0; i < array.length; i++) {
    int randomPosition = random.nextInt(array.length);
    int temp = array[i];
    array[i] = array[randomPosition];
    array[randomPosition] = temp;
return array;
voidshuffle(int[] array)
Shuffles an integer array by the same method as in java.util.Collections.shuffle
shuffle(array, new java.util.Random());
voidshuffle(int[] array)
Shuffles the array given.
for (int i = 0; i < array.length - 1; i++) {
    int j = rand.nextInt(i + 1);
    int temp = array[j];
    array[j] = array[i];
    array[i] = temp;
int[]shuffle(int[] array)
Fisher-Yates implementation which shuffles the array contents.
int index;
Random random = new Random(42);
for (int i = array.length - 1; i > 0; i--) {
    index = random.nextInt(i + 1);
    if (index != i) {
        array[index] ^= array[i];
        array[i] ^= array[index];
        array[index] ^= array[i];
...
voidshuffle(int[] array)
Shuffle the values of an int array into random order
if (array == null) {
    return;
for (int i = 0; i < array.length; i++) {
    int r = i + random.nextInt(array.length - i);
    int tmp = array[i];
    array[i] = array[r];
    array[r] = tmp;
...
voidshuffle(int[] array, Random rand)
shuffle
shuffle(array, rand, array.length);
voidshuffle(int[] array, Random rand)
Swaps values in the given array
shuffle(array, 0, array.length, rand);
int[]shuffle(int[] array, Random rnd)
shuffle
for (int i = array.length - 1; i > 0; i--) {
    int pos = rnd.nextInt(i + 1);
    int t = array[pos];
    array[pos] = array[i];
    array[i] = t;
return array;