Here you can find the source of shuffle(int[] array, Random rand)
Parameter | Description |
---|---|
array | the array to swap values in |
rand | the source of randomness for shuffling |
static public void shuffle(int[] array, Random rand)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { /**/*from w w w . j av a 2 s. c om*/ * Swaps values in the given array * * @param array the array to swap values in * @param rand the source of randomness for shuffling */ static public void shuffle(int[] array, Random rand) { shuffle(array, 0, array.length, rand); } /** * Shuffles the values in the given array * @param array the array to shuffle values in * @param from the first index, inclusive, to shuffle from * @param to the last index, exclusive, to shuffle from * @param rand the source of randomness for shuffling */ static public void shuffle(int[] array, int from, int to, Random rand) { for (int i = to - 1; i > from; i--) swap(array, i, rand.nextInt(i)); } /** * Swaps two indices in the given array * @param array the array to perform the sawp in * @param a the first index * @param b the second index */ static public void swap(int[] array, int a, int b) { int tmp = array[a]; array[a] = array[b]; array[b] = tmp; } }