Java Utililty Methods Array Shuffle

List of utility methods to do Array Shuffle

Description

The list of methods to do Array Shuffle are organized into topic(s).

Method

Ashuffle(final A source, final Random random)
shuffle
Collections.shuffle(source, random);
return source;
voidshuffle(final Object[] array)
shuffle
final Random r = new Random();
final int limit = array.length;
for (int i = 0; i < limit; ++i) {
    swap(array, i, r.nextInt(limit));
voidshuffle(final Object[] values)
shuffle
for (int i = 0; i != values.length; i++) {
    final int r = generator.nextInt(i + 1);
    swap(values, i, r);
voidshuffle(final T[] arr)
shuffle
Random rnd = new Random();
for (int i = arr.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    T tmp = arr[index];
    arr[index] = arr[i];
    arr[i] = tmp;
T[]shuffle(final T[] array)
Shuffle the elements in the given array.
if (array != null && array.length > 0) {
    final Random random = getRandom();
    for (int n = array.length; n > 1;) {
        final int randomIndex = random.nextInt(n);
        n--;
        if (n != randomIndex) {
            final T tmp = array[randomIndex];
            array[randomIndex] = array[n];
...
voidshuffle(final T[] array)
shuffle
shuffle(array, array.length);
voidshuffle(int a[])
Shuffles (randomizes) the contents of the given array.
for (int i = 0; i < a.length - 1; i++) {
    int tmp = a[i];
    int index = i + 1 + random.nextInt(a.length - i - 1);
    a[i] = a[index];
    a[index] = tmp;
voidshuffle(int arr[])
shuffle
Random rnd = r;
if (rnd == null) {
    r = rnd = new Random();
for (int i = arr.length; i > 1; i--) {
    int nextInt = rnd.nextInt(i);
    int tmp = arr[i - 1];
    arr[i - 1] = arr[nextInt];
...
ArrayListshuffle(int size, boolean reallyShuffle)
shuffle
ArrayList<Integer> result = new ArrayList<Integer>();
if (reallyShuffle) {
    HashSet<Integer> shuffled = new HashSet<Integer>();
    Random r = new Random();
    while (result.size() != size) {
        int i = r.nextInt(size);
        if (shuffled.add(i)) {
            result.add(i);
...
voidshuffle(int[] a)
shuffle
for (int i = a.length - 1; i > 0; i--) {
    int j = rand.nextInt(i + 1);
    swap(a, i, j);