Here you can find the source of shuffle(List
Parameter | Description |
---|---|
T | the type of the list (implicit). |
list | the list to shuffle. |
nswaps | the number of swaps to perform. |
public static <T> void shuffle(List<T> list, int nswaps)
//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); } } }