Java Array Shuffle shuffle(byte[] values, Random rnd)

Here you can find the source of shuffle(byte[] values, Random rnd)

Description

Shuffles the elements in the given array into a random sequence.

License

Open Source License

Parameter

Parameter Description
values the array to shuffle.
rnd the source from which random values for shuffling the array are obtained.

Declaration

public static void shuffle(byte[] values, Random rnd) 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    /** The default random object used when shuffling an array. */
    protected static final Random _rnd = new Random();

    /**// w  ww .  j av a 2s.co m
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     */
    public static void shuffle(byte[] values) {
        shuffle(values, _rnd);
    }

    /**
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(byte[] values, Random rnd) {
        shuffle(values, 0, values.length, rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     */
    public static void shuffle(byte[] values, int offset, int length) {
        shuffle(values, offset, length, _rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(byte[] values, int offset, int length, Random rnd) {
        // starting from the end of the specified region, repeatedly swap
        // the element in question with a random element previous to it
        // (in the specified region) up to and including itself
        for (int ii = offset + length - 1; ii > offset; ii--) {
            int idx = offset + rnd.nextInt(ii - offset + 1);
            byte tmp = values[ii];
            values[ii] = values[idx];
            values[idx] = tmp;
        }
    }

    /**
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     */
    public static void shuffle(int[] values) {
        shuffle(values, _rnd);
    }

    /**
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(int[] values, Random rnd) {
        shuffle(values, 0, values.length, rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     */
    public static void shuffle(int[] values, int offset, int length) {
        shuffle(values, offset, length, _rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(int[] values, int offset, int length, Random rnd) {
        // starting from the end of the specified region, repeatedly swap
        // the element in question with a random element previous to it
        // (in the specified region) up to and including itself
        for (int ii = offset + length - 1; ii > offset; ii--) {
            int idx = offset + rnd.nextInt(ii - offset + 1);
            int tmp = values[ii];
            values[ii] = values[idx];
            values[idx] = tmp;
        }
    }

    /**
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     */
    public static void shuffle(Object[] values) {
        shuffle(values, _rnd);
    }

    /**
     * Shuffles the elements in the given array into a random sequence.
     *
     * @param values the array to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(Object[] values, Random rnd) {
        shuffle(values, 0, values.length, rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     */
    public static void shuffle(Object[] values, int offset, int length) {
        shuffle(values, offset, length, _rnd);
    }

    /**
     * Shuffles a subset of elements within the specified array into a random sequence.
     *
     * @param values the array containing elements to shuffle.
     * @param offset the index at which to start shuffling elements.
     * @param length the number of elements to shuffle.
     * @param rnd the source from which random values for shuffling the array are obtained.
     */
    public static void shuffle(Object[] values, int offset, int length, Random rnd) {
        // starting from the end of the specified region, repeatedly swap
        // the element in question with a random element previous to it
        // (in the specified region) up to and including itself
        for (int ii = offset + length - 1; ii > offset; ii--) {
            int idx = offset + rnd.nextInt(ii - offset + 1);
            Object tmp = values[ii];
            values[ii] = values[idx];
            values[idx] = tmp;
        }
    }
}

Related

  1. randomize(final T[] array)
  2. shuffle()
  3. shuffle(byte[] array)
  4. shuffle(Collection items, Random random)
  5. shuffle(Collection objects)
  6. shuffle(double[] arr, Random rnd)
  7. Shuffle(double[] array)