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

voidshuffleArray(int[] ar)
shuffle Array
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
voidshuffleArray(int[] ar)
shuffle Array
Random rnd = new Random();
for (int i = ar.length - 1; i >= 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
voidshuffleArray(int[] ar, Random rnd, int mx)
shuffle Array
for (int i = mx; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
voidshuffleArray(int[] arr)
shuffle Array
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; i++) {
    int tmp = arr[i];
    int randomPos = i + rnd.nextInt(n - i);
    arr[i] = arr[randomPos];
    arr[randomPos] = tmp;
voidshuffleArray(int[] array, int length)
Shuffle an array or a portion of an array
int index, temp;
Random random = new Random();
for (int i = length - 1; i > 0; i--) {
    index = random.nextInt(i + 1);
    temp = array[index];
    array[index] = array[i];
    array[i] = temp;
voidshuffleArray(int[] array, long rngSeed)
shuffle Array
shuffleArray(array, new Random(rngSeed));
voidshuffleArray(List list)
shuffle Array
Random rand = new Random();
for (int i = list.size() - 1; i > 0; i--) {
    int index = rand.nextInt(i + 1);
    swap(list, index, i);
voidshuffleArray(Object[] array)
shuffle Array
shuffleArray(array, new Random());
voidshuffleArray(T[] ar)
shuffle Array
for (int i = ar.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    T a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
voidshuffleArray(T[] arr)
Permutes the elements of the given array.
for (int i = arr.length; i > 1; i--)
    swap(arr, i - 1, rand.nextInt(i));