Java Array Shuffle shuffle(T[] array, Random rand)

Here you can find the source of shuffle(T[] array, Random rand)

Description

Randomly shuffle an array in place.

License

Open Source License

Parameter

Parameter Description
T the type of the elements
array the array to shuffle
rand the random number generator

Declaration

public static <T> void shuffle(T[] array, Random rand) 

Method Source Code

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

import java.util.Random;

public class Main {
    /**/* w  ww  .  j  av a  2s .  c o  m*/
     * Randomly shuffle an array in place.
     *
     * @param <T> the type of the elements
     * @param array the array to shuffle
     * @param rand the random number generator
     */
    public static <T> void shuffle(T[] array, Random rand) {
        for (int i = 0; i < array.length; i++) {
            int index = rand.nextInt(array.length);
            // Simple swap
            T temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }
}

Related

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