Java Array Shuffle shuffle(List list, int nswaps)

Here you can find the source of shuffle(List list, int nswaps)

Description

shuffle

License

Open Source License

Parameter

Parameter Description
T the type of the list (implicit).
list the list to shuffle.
nswaps the number of swaps to perform.

Declaration

public static <T> void shuffle(List<T> list, int nswaps) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU Lesser General Public License as published by

import java.util.List;
import java.util.Random;

public class Main {
    /**/*from w  w  w  . j a  v  a2 s. c o  m*/
     * @param <T>
     *            the type of the list (implicit).
     * @param list
     *            the list to shuffle.
     * @param nswaps
     *            the number of swaps to perform.
     */
    public static <T> void shuffle(List<T> list, int nswaps) {
        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);
        }
    }
}

Related

  1. shuffle(List list)
  2. shuffle(List list, Random rnd)
  3. shuffle(List as)
  4. shuffle(List list)
  5. shuffle(List list)
  6. shuffle(Object[] a)
  7. shuffle(Object[] a, Random r)
  8. shuffle(Object[] array)
  9. shuffle(Object[] objs, Random random, int start, int len)