Here you can find the source of shuffle(Object[] a)
public static void shuffle(Object[] a)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { private static Random random; /**//from w w w . j av a 2s . c o m * Rearrange the elements of an array in random order. */ public static void shuffle(Object[] a) { int N = a.length; for (int i = 0; i < N; i++) { int r = i + uniform(N - i); // between i and N-1 Object temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Rearrange the elements of a double array in random order. */ public static void shuffle(double[] a) { int N = a.length; for (int i = 0; i < N; i++) { int r = i + uniform(N - i); // between i and N-1 double temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Rearrange the elements of an int array in random order. */ public static void shuffle(int[] a) { int N = a.length; for (int i = 0; i < N; i++) { int r = i + uniform(N - i); // between i and N-1 int temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Rearrange the elements of the subarray a[lo..hi] in random order. */ public static void shuffle(Object[] a, int lo, int hi) { if (lo < 0 || lo > hi || hi >= a.length) { throw new IndexOutOfBoundsException("Illegal subarray range"); } for (int i = lo; i <= hi; i++) { int r = i + uniform(hi - i + 1); // between i and hi Object temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Rearrange the elements of the subarray a[lo..hi] in random order. */ public static void shuffle(double[] a, int lo, int hi) { if (lo < 0 || lo > hi || hi >= a.length) { throw new IndexOutOfBoundsException("Illegal subarray range"); } for (int i = lo; i <= hi; i++) { int r = i + uniform(hi - i + 1); // between i and hi double temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Rearrange the elements of the subarray a[lo..hi] in random order. */ public static void shuffle(int[] a, int lo, int hi) { if (lo < 0 || lo > hi || hi >= a.length) { throw new IndexOutOfBoundsException("Illegal subarray range"); } for (int i = lo; i <= hi; i++) { int r = i + uniform(hi - i + 1); // between i and hi int temp = a[i]; a[i] = a[r]; a[r] = temp; } } /** * Return real number uniformly in [0, 1). */ public static double uniform() { return random.nextDouble(); } /** * Returns an integer uniformly between 0 (inclusive) and N (exclusive). * @throws IllegalArgumentException if <tt>N <= 0</tt> */ public static int uniform(int N) { if (N <= 0) throw new IllegalArgumentException("Parameter N must be positive"); return random.nextInt(N); } /** * Returns an integer uniformly in [a, b). * @throws IllegalArgumentException if <tt>b <= a</tt> * @throws IllegalArgumentException if <tt>b - a >= Integer.MAX_VALUE</tt> */ public static int uniform(int a, int b) { if (b <= a) throw new IllegalArgumentException("Invalid range"); if ((long) b - a >= Integer.MAX_VALUE) throw new IllegalArgumentException("Invalid range"); return a + uniform(b - a); } /** * Returns a real number uniformly in [a, b). * @throws IllegalArgumentException unless <tt>a < b</tt> */ public static double uniform(double a, double b) { if (!(a < b)) throw new IllegalArgumentException("Invalid range"); return a + uniform() * (b - a); } }