Java Array Shuffle shuffleArray(int arr[])

Here you can find the source of shuffleArray(int arr[])

Description

shuffle Array

License

Open Source License

Declaration

public static int[] shuffleArray(int arr[]) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    private static Random RANDOM = new Random(System.currentTimeMillis());

    public static int[] shuffleArray(int arr[]) {
        if (arr == null || arr.length == 0)
            return arr;

        /**/*from w  ww  .j  av  a  2s  . c  o m*/
         * Shuffle the available columns. 
         * Fisher-Yates shuffle a.k.a. the Knuth shuffle, modern version.
         */
        for (int i = arr.length - 1; i >= 1; i--) {
            /* +1 because the n argument is exclusive */
            int j = RANDOM.nextInt(i + 1);
            // swap a[j] and a[i]
            int tmp = arr[j];
            arr[j] = arr[i];
            arr[i] = tmp;
        }
        return arr;
    }
}

Related

  1. shuffle(T[] array, Random rand)
  2. shuffle(T[] array, Random rand)
  3. shuffle(T[] array, Random random)
  4. shuffle(T[] data)
  5. shuffleArray(double[] ar)
  6. shuffleArray(int[] a)
  7. shuffleArray(int[] ar)
  8. shuffleArray(int[] ar)
  9. shuffleArray(int[] ar, Random rnd, int mx)