Here you can find the source of shuffle(T[] array)
Parameter | Description |
---|---|
array | a parameter |
public static <T> void shuffle(T[] array)
//package com.java2s; import java.util.Random; public class Main { /**//from www . jav a2 s .c o m * Rearranges the array items in random order using the default * java.util.Random generator. Operation is in-place, no copy is created. * * @param array */ public static <T> void shuffle(T[] array) { shuffle(array, new Random()); } /** * Rearranges the array items in random order using the given RNG. Operation * is in-place, no copy is created. * * @param array * @param rnd */ public static <T> void shuffle(T[] array, Random rnd) { int N = array.length; for (int i = 0; i < N; i++) { int r = i + rnd.nextInt(N - i); // between i and N-1 T swap = array[i]; array[i] = array[r]; array[r] = swap; } } }