Java Array Shuffle shuffleList(List list)

Here you can find the source of shuffleList(List list)

Description

Shuffles the input list using an implementation of the Knuth "Algorithm P" shuffle.

License

Open Source License

Declaration

public static void shuffleList(List<Integer> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from   ww w  .j  ava  2 s.com*/
     * Shuffles the input list using an implementation of the Knuth "Algorithm P" shuffle.
     * 
     * This  algorithm produces a random permutation of the elements
     * in the input list. See Introduction to Algorithms page 127 for proof.
     */
    public static void shuffleList(List<Integer> list) {
        Random randomGenerator = new Random();
        for (int loopIndex = list.size() - 1; loopIndex > 0; loopIndex--) {
            int randomIndex = randomGenerator.nextInt(loopIndex + 1);
            Collections.swap(list, randomIndex, loopIndex);
        }
    }
}

Related

  1. shuffleInPlace(E[] elems, Random rand)
  2. shuffleInPlace(int[] toShuffle, Random random)
  3. shuffleIntArray(int[] arr)
  4. shuffleIntArray(int[] array)
  5. shuffleList(List list)
  6. shuffleSublist(List objects, int start, int end)