List of utility methods to do Array Shuffle
List | shuffled(List shuffled Collections.shuffle(list, random);
return list;
|
List | shuffledList(List shuffledList ArrayList<T> result = new ArrayList<T>(list); Collections.shuffle(result); return result; |
void | shuffleInPlace(E[] elems, Random rand) shuffle In Place for (int j = elems.length - 1; j > 0; j--) { int randIndex = rand.nextInt(j + 1); E tmp = elems[randIndex]; elems[randIndex] = elems[j]; elems[j] = tmp; |
void | shuffleInPlace(int[] toShuffle, Random random) Randomly shuffle the specified integer array using a Fisher-Yates shuffle algorithm for (int i = 0; i < toShuffle.length - 1; i++) { int j = i + random.nextInt(toShuffle.length - i); int temp = toShuffle[i]; toShuffle[i] = toShuffle[j]; toShuffle[j] = temp; |
void | shuffleIntArray(int[] arr) shuffle Int Array if (arr == null || arr.length == 0) throw new IllegalArgumentException("Array cannot be null or have zero length"); for (int i = arr.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); int a = arr[index]; arr[index] = arr[i]; arr[i] = a; |
int[] | shuffleIntArray(int[] array) shuffle Int Array int[] copy = array.clone(); Random rnd = new Random(); for (int i = copy.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); int a = copy[index]; copy[index] = copy[i]; copy[i] = a; return copy; |
void | shuffleList(List> list) Shuffles a list. long seed = System.nanoTime(); Collections.shuffle(list, new Random(seed)); |
void | shuffleList(List Shuffles the input list using an implementation of the Knuth "Algorithm P" shuffle. Random randomGenerator = new Random(); for (int loopIndex = list.size() - 1; loopIndex > 0; loopIndex--) { int randomIndex = randomGenerator.nextInt(loopIndex + 1); Collections.swap(list, randomIndex, loopIndex); |
void | shuffleSublist(List Shuffle the elements between the specified start and end index both inclusive. for (int index = start; index <= end; index++) { T current = objects.get(index); int randomIndex = nextInt(start, end); T randomObject = objects.get(randomIndex); objects.set(index, randomObject); objects.set(randomIndex, current); |