Java Array Shuffle shuffle(short... a)

Here you can find the source of shuffle(short... a)

Description

Shuffles the given array randomizing the elements.

License

Open Source License

Parameter

Parameter Description
a the array to shuffle

Return

the shuffled array

Declaration

public static short[] shuffle(short... a) 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    private static final Random RAND = new Random();

    /**//from   w w  w . j  a v  a  2s .  c  o m
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static int[] shuffle(int... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            int tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static short[] shuffle(short... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            short tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static long[] shuffle(long... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            long tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static float[] shuffle(float... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            float tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static double[] shuffle(double... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            double tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static char[] shuffle(char... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            char tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }

    /**
     * Shuffles the given array randomizing the elements. The argument and return
     * values are the same for reference.
     *
     * @param a the array to shuffle
     * @return the shuffled array
     */
    public static <T> T[] shuffle(final T... a) {
        for (int i = 0; i < a.length; i++) {
            int p = RAND.nextInt(a.length);
            T tmp = a[i];
            a[i] = a[p];
            a[p] = tmp;
        }
        return a;
    }
}

Related

  1. shuffle(Object[] a)
  2. shuffle(Object[] a, Random r)
  3. shuffle(Object[] array)
  4. shuffle(Object[] objs, Random random, int start, int len)
  5. shuffle(Random rand, O[] array)
  6. shuffle(String str, Random randObj)
  7. shuffle(T[] a, int from, int to, Random rnd)
  8. shuffle(T[] array)
  9. shuffle(T[] array)