Java Array Shuffle shuffle(int[] array)

Here you can find the source of shuffle(int[] array)

Description

shuffle

License

Apache License

Declaration

public static int[] shuffle(int[] array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    public static Random random = new Random();

    public static int[] shuffle(int[] array) {

        for (int i = 0; i < array.length; i++) {
            int randomPosition = random.nextInt(array.length);
            int temp = array[i];
            array[i] = array[randomPosition];
            array[randomPosition] = temp;
        }/*w ww. j  ava  2  s  .com*/

        return array;
    }

    public static void shuffle(long[] array, int startIndex, int endIndex) {
        assert (endIndex <= array.length && startIndex >= 0 && endIndex > 0);
        for (int i = startIndex; i < endIndex; i++) {
            int randomPosition = random.nextInt(endIndex - startIndex) + startIndex;
            long temp = array[i];
            array[i] = array[randomPosition];
            array[randomPosition] = temp;
        }

    }
}

Related

  1. shuffle(int size, boolean reallyShuffle)
  2. shuffle(int[] a)
  3. shuffle(int[] arr)
  4. shuffle(int[] arr)
  5. shuffle(int[] array)
  6. shuffle(int[] array)
  7. shuffle(int[] array)
  8. shuffle(int[] array)
  9. shuffle(int[] array, Random rand)