Java examples for Collection Framework:Array Random Value
shuffle an int array
//package com.java2s; import java.util.Random; public class Main { public static void main(String[] argv) { int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(java.util.Arrays.toString(shuffle(array))); }/* www .j a v a 2 s .co m*/ public static Random random = new Random(); public static int[] shuffle(int[] array) { for (int i = 0; i < array.length; i++) { int randomPosition = random.nextInt(array.length); int temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static void shuffle(long[] array, int startIndex, int endIndex) { assert (endIndex <= array.length && startIndex >= 0 && endIndex > 0); for (int i = startIndex; i < endIndex; i++) { int randomPosition = random.nextInt(endIndex - startIndex) + startIndex; long temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } } }