Java Array Shuffle Shuffle(double[] array)

Here you can find the source of Shuffle(double[] array)

Description

Shuffle an array.

License

BSD License

Parameter

Parameter Description
array Array.

Declaration

public static void Shuffle(double[] array) 

Method Source Code

//package com.java2s;
// under the BSD license. The original license terms are given below:

import java.util.Random;

public class Main {
    /**//from  w  w w .  j  a v  a2s  .  co m
     * Shuffle an array.
     * @param array Array.
     */
    public static void Shuffle(double[] array) {
        Shuffle(array, 0);
    }

    /**
     * Shuffle an array.
     * @param array Array.
     * @param seed Random seed.
     */
    public static void Shuffle(double[] array, long seed) {
        Random random = new Random();
        if (seed != 0)
            random.setSeed(seed);

        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            double temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }

    /**
     * Shuffle an array.
     * @param array Array.
     */
    public static void Shuffle(int[] array) {
        Shuffle(array, 0);
    }

    /**
     * Shuffle an array.
     * @param array Array.
     * @param seed Seed of the random.
     */
    public static void Shuffle(int[] array, long seed) {

        Random random = new Random();
        if (seed != 0)
            random.setSeed(seed);

        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            int temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }

    /**
     * Shuffle an array.
     * @param array Array.
     * @param seed Random seed.
     */
    public static void Shuffle(float[] array, long seed) {
        Random random = new Random();
        if (seed != 0)
            random.setSeed(seed);

        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            float temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }
}

Related

  1. shuffle(byte[] array)
  2. shuffle(byte[] values, Random rnd)
  3. shuffle(Collection items, Random random)
  4. shuffle(Collection objects)
  5. shuffle(double[] arr, Random rnd)
  6. shuffle(double[] array, Random r)
  7. shuffle(double[][] kernel, long seed)
  8. shuffle(final A source, final Random random)
  9. shuffle(final Object[] array)