Java Array Shuffle shuffleArray(int[] array, int length)

Here you can find the source of shuffleArray(int[] array, int length)

Description

Shuffle an array or a portion of an array

License

Open Source License

Parameter

Parameter Description
array The array to be shuffled
length The last index of the portion of the array to be shuffled

Declaration

public static void shuffleArray(int[] array, int length) 

Method Source Code


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

import java.util.Random;

public class Main {
    /**/*w  ww  .j  a  v  a2s  . c  o m*/
     * Shuffle an array or a portion of an array
     *
     * @param array  The array to be shuffled
     * @param length The last index of the portion of the array to be shuffled
     */
    public static void shuffleArray(int[] array, int length) {
        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;
        }
    }
}

Related

  1. shuffleArray(int[] a)
  2. shuffleArray(int[] ar)
  3. shuffleArray(int[] ar)
  4. shuffleArray(int[] ar, Random rnd, int mx)
  5. shuffleArray(int[] arr)
  6. shuffleArray(int[] array, long rngSeed)
  7. shuffleArray(List list)
  8. shuffleArray(Object[] array)
  9. shuffleArray(T[] ar)