Java Array Shuffle shuffle(double[] array, Random r)

Here you can find the source of shuffle(double[] array, Random r)

Description

Implements Fisher-Yates shuffle.

License

Open Source License

Parameter

Parameter Description
array Array to be shuffled
r Random generator

Declaration

public static void shuffle(double[] array, Random r) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from w w  w  .  j  av  a2s.c o  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.Random;

public class Main {
    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * 
     */
    public static void shuffle(double[] array) {
        Random r = new Random();
        shuffle(array, r);
    }

    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * @param r Random generator
     * 
     */
    public static void shuffle(double[] array, Random r) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = r.nextInt(i + 1);

            double a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
    }

    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * 
     */
    public static void shuffle(int[] array) {
        Random r = new Random();
        shuffle(array, r);
    }

    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * @param r Random generator
     * 
     */
    public static void shuffle(int[] array, Random r) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = r.nextInt(i + 1);

            int a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
    }

    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * 
     */
    public static void shuffle(long[] array) {
        Random r = new Random();
        shuffle(array, r);
    }

    /**
     * Implements Fisher-Yates shuffle.
     *
     * @param array Array to be shuffled
     * @param r Random generator
     * 
     */
    public static void shuffle(long[] array, Random r) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = r.nextInt(i + 1);

            long a = array[index];
            array[index] = array[i];
            array[i] = a;
        }
    }
}

Related

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