Here you can find the source of shuffle(double[] array, Random r)
Parameter | Description |
---|---|
array | Array to be shuffled |
r | Random generator |
public static void shuffle(double[] array, Random r)
//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; } } }