Here you can find the source of shuffle(int[] arr)
public static int[] shuffle(int[] arr)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { public static int[] shuffle(int[] arr) { if (arr == null) { return new int[0]; }//from w w w . j ava 2s . c om return random(arr, arr.length); } public static char[] shuffle(char[] arr) { if (arr == null) { return new char[0]; } return random(arr, arr.length); } public static byte[] shuffle(byte[] arr) { if (arr == null) { return new byte[0]; } return random(arr, arr.length); } public static Object[] shuffle(Object[] arr) { if (arr == null) { return new Object[0]; } return random(arr, arr.length); } public static String[] shuffle(String[] arr) { if (arr == null) { return new String[0]; } return random(arr, arr.length); } public static int[] random(int[] arr) { if (arr == null) { return new int[0]; } int count = new Random().nextInt(arr.length); count = count > 0 ? count : 1; return random(arr, count); } public static int[] random(int[] arr, int count) { int[] rarr = new int[0]; if (arr != null) { if (arr.length < count) { count = arr.length; } Random random = new Random(); int i = 0; int num = 0; rarr = new int[count]; int[] keys = new int[count]; for (int j = 0; j < keys.length; j++) { keys[j] = -1; } do { num = random.nextInt(arr.length); if (!inArray(keys, num)) { keys[i] = num; rarr[i] = arr[num]; i++; } } while (i < count); } return rarr; } public static char[] random(char[] arr) { if (arr == null) { return new char[0]; } int count = new Random().nextInt(arr.length); count = count > 0 ? count : 1; return random(arr, count); } public static char[] random(char[] arr, int count) { char[] rarr = new char[0]; if (arr != null) { if (arr.length < count) { count = arr.length; } Random random = new Random(); int i = 0; int num = 0; rarr = new char[count]; int[] keys = new int[count]; for (int j = 0; j < keys.length; j++) { keys[j] = 0; } do { num = random.nextInt(arr.length); if (!inArray(keys, num)) { keys[i] = num; rarr[i] = arr[num]; i++; } } while (i < count); } return rarr; } public static byte[] random(byte[] arr) { if (arr == null) { return new byte[0]; } int count = new Random().nextInt(arr.length); count = count > 0 ? count : 1; return random(arr, count); } public static byte[] random(byte[] arr, int count) { byte[] rarr = new byte[0]; if (arr != null) { if (arr.length < count) { count = arr.length; } Random random = new Random(); int i = 0; int num = 0; rarr = new byte[count]; int[] keys = new int[count]; for (int j = 0; j < keys.length; j++) { keys[j] = 0; } do { num = random.nextInt(arr.length); if (!inArray(keys, num)) { keys[i] = num; rarr[i] = arr[num]; i++; } } while (i < count); } return rarr; } public static Object[] random(Object[] arr) { if (arr == null) { return new Object[0]; } int count = new Random().nextInt(arr.length); count = count > 0 ? count : 1; return random(arr, count); } public static Object[] random(Object[] arr, int count) { Object[] rarr = new Object[0]; if (arr != null) { if (arr.length < count) { count = arr.length; } Random random = new Random(); int i = 0; int num = 0; rarr = new Object[count]; int[] keys = new int[count]; for (int j = 0; j < keys.length; j++) { keys[j] = -1; } do { num = random.nextInt(arr.length); if (!inArray(keys, num)) { keys[i] = num; rarr[i] = arr[num]; i++; } } while (i < count); } return rarr; } public static String[] random(String[] arr) { if (arr == null) { return new String[0]; } int count = new Random().nextInt(arr.length); count = count > 0 ? count : 1; return random(arr, count); } public static String[] random(String[] arr, int count) { String[] rarr = new String[0]; if (arr != null) { if (arr.length < count) { count = arr.length; } Random random = new Random(); int i = 0; int num = 0; rarr = new String[count]; int[] keys = new int[count]; for (int j = 0; j < keys.length; j++) { keys[j] = -1; } do { num = random.nextInt(arr.length); if (!inArray(keys, num)) { keys[i] = num; rarr[i] = arr[num]; i++; } } while (i < count); } return rarr; } public static boolean inArray(int[] arr, int search) { if (arr != null) { for (int i = 0; i < arr.length; i++) { if (arr[i] == search) { return true; } } } return false; } public static boolean inArray(int[] arr, int[] search) { if (search != null) { for (int i = 0; i < search.length; i++) { if (!inArray(arr, search[i])) { return false; } } return true; } return false; } public static boolean inArray(char[] arr, char search) { if (arr != null) { for (int i = 0; i < arr.length; i++) { if (arr[i] == search) { return true; } } } return false; } public static boolean inArray(char[] arr, char[] search) { if (search != null) { for (int i = 0; i < search.length; i++) { if (!inArray(arr, search[i])) { return false; } } return true; } return false; } public static boolean inArray(byte[] arr, byte search) { if (arr != null) { for (int i = 0; i < arr.length; i++) { if (arr[i] == search) { return true; } } } return false; } public static boolean inArray(byte[] arr, byte[] search) { if (search != null) { for (int i = 0; i < search.length; i++) { if (!inArray(arr, search[i])) { return false; } } return true; } return false; } public static boolean inArray(Object[] arr, Object search) { if (arr != null && search != null) { for (int i = 0; i < arr.length; i++) { if (search.equals(arr[i])) { return true; } } } return false; } public static boolean inArray(Object[] arr, Object[] search) { if (search != null) { for (int i = 0; i < search.length; i++) { if (!inArray(arr, search[i])) { return false; } } return true; } return false; } public static boolean inArray(String[] arr, String search) { if (arr != null && search != null) { for (int i = 0; i < arr.length; i++) { if (search.equals(arr[i])) { return true; } } } return false; } public static boolean inArray(String[] arr, String[] search) { if (search != null) { for (int i = 0; i < search.length; i++) { if (!inArray(arr, search[i])) { return false; } } return true; } return false; } }