Java Array Shuffle shuffle(int[] array, Random rand)

Here you can find the source of shuffle(int[] array, Random rand)

Description

Swaps values in the given array

License

Open Source License

Parameter

Parameter Description
array the array to swap values in
rand the source of randomness for shuffling

Declaration

static public void shuffle(int[] array, Random rand) 

Method Source Code


//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;
    }
}

Related

  1. shuffle(int[] array)
  2. shuffle(int[] array)
  3. shuffle(int[] array)
  4. shuffle(int[] array)
  5. shuffle(int[] array, Random rand)
  6. shuffle(int[] array, Random rnd)
  7. shuffle(int[] ary)
  8. shuffle(int[] input)
  9. shuffle(int[] list, Random rnd)