Java ThreadLocalRandom shuffle(T[] array)

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

Description

An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.

License

Open Source License

Parameter

Parameter Description
array The array that will be shuffled.

Return

The shuffled array.

Declaration

public static <T> T[] shuffle(T[] array) 

Method Source Code

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

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**//w w w  . j  av a2  s . c o  m
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code T} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static <T> T[] shuffle(T[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            T a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code int} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static int[] shuffle(int[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            int a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code long} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static long[] shuffle(long[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            long a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code double} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static double[] shuffle(double[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            double a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code short} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static short[] shuffle(short[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            short a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code byte} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static byte[] shuffle(byte[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            byte a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code float} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static float[] shuffle(float[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            float a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code boolean} array.
     *
     * @param array The array that will be shuffled.
     * @return The shuffled array.
     */
    public static boolean[] shuffle(boolean[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            boolean a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }

    /**
     * An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an {@code char} array.
     *
     * @param array The array that will be shuffled.
     */
    public static char[] shuffle(char[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            char a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
        return array;
    }
}

Related

  1. randomStringArray(int arrayLength, int stringLength)
  2. randomWorld()
  3. randomWorld()
  4. shuffle(final T[] array)
  5. shuffle(T[] arr)
  6. shuffleArray(int[] ar)
  7. timestampToMicros(final long timestampMillis)