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

Listshuffle(List list)
Method to shuffle the list
if (list != null && list.size() > 0) {
    List<T> updatedList = new ArrayList<>();
    final int[] ints = new Random().ints(0, list.size()).distinct().limit(list.size()).toArray();
    for (int i = 0; i < ints.length; i++) {
        updatedList.add(list.get(i));
    return updatedList;
return null;
voidshuffle(List list, int nswaps)
shuffle
int size = list.size();
Random random = new Random();
for (int i = 0; i < nswaps; i++) {
    int a = random.nextInt(size);
    int b = random.nextInt(size);
    T t = list.get(a);
    list.set(a, list.get(b));
    list.set(b, t);
...
voidshuffle(Object[] a)
Rearrange the elements of an array in random order.
int N = a.length;
for (int i = 0; i < N; i++) {
    int r = i + uniform(N - i); 
    Object temp = a[i];
    a[i] = a[r];
    a[r] = temp;
voidshuffle(Object[] a, Random r)
shuffle array
for (int n = 0; n < a.length; n++) {
    int x = r.nextInt(a.length - n) + n;
    Object o = a[n];
    a[n] = a[x];
    a[x] = o;
voidshuffle(Object[] array)
shuffle
for (int i = 0; i < array.length; i++) {
    int nuovaPosizione = RANDOM.nextInt(array.length);
    Object swap = array[nuovaPosizione];
    array[nuovaPosizione] = array[i];
    array[i] = swap;
voidshuffle(Object[] objs, Random random, int start, int len)
shuffle
for (int i = 0; i < len; i++) {
    int j = random.nextInt(len);
    Object tmp = objs[i];
    objs[i] = objs[j];
    objs[j] = tmp;
voidshuffle(Random rand, O[] array)
shuffle
for (int i = array.length - 1; i > 0; i--) {
    int j = rand.nextInt(i + 1);
    O temp = array[i];
    array[i] = array[j];
    array[j] = temp;
short[]shuffle(short... a)
Shuffles the given array randomizing the elements.
for (int i = 0; i < a.length; i++) {
    int p = RAND.nextInt(a.length);
    short tmp = a[i];
    a[i] = a[p];
    a[p] = tmp;
return a;
Stringshuffle(String str, Random randObj)
Single nucleotide shuffle
if (str.length() <= 1)
    return str;
int split = str.length() / 2;
String temp1 = shuffle(str.substring(0, split), randObj);
String temp2 = shuffle(str.substring(split), randObj);
if (randObj.nextDouble() > 0.5)
    return temp1 + temp2;
else
...
T[]shuffle(T[] a, int from, int to, Random rnd)
Randomly permute the specified array from using the specified source of randomness.
for (int i = to; i > from + 1; i--) {
    swap(a, i - 1, rnd.nextInt(i - from) + from);
return a;