Class Shuffler

java.lang.Object
org.cicirello.math.rand.Shuffler

public final class Shuffler extends Object
Shuffler is a class of utility methods for randomizing the order of elements in arrays and Lists.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    shuffle(byte[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(byte[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(byte[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(byte[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(char[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(char[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(char[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(char[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(double[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(double[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(double[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(double[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(float[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(float[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(float[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(float[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(int[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(int[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(int[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(int[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(long[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(long[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(long[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(long[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(short[] array)
    Randomizes the ordering of the elements of an array.
    static void
    shuffle(short[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(short[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static void
    shuffle(short[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.
    static <T> void
    shuffle(List<T> list)
    Randomizes the ordering of the elements of a List.
    static <T> void
    shuffle(List<T> list, int first, int last)
    Randomizes the ordering of the elements of a portion of a List.
    static <T> void
    shuffle(List<T> list, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements of a portion of a List.
    static <T> void
    shuffle(List<T> list, RandomGenerator gen)
    Randomizes the ordering of the elements of a List.
    static <T> void
    shuffle(T[] array)
    Randomizes the ordering of the elements of an array.
    static <T> void
    shuffle(T[] array, int first, int last)
    Randomizes the ordering of the elements within a portion of an array.
    static <T> void
    shuffle(T[] array, int first, int last, RandomGenerator gen)
    Randomizes the ordering of the elements within a portion of an array.
    static <T> void
    shuffle(T[] array, RandomGenerator gen)
    Randomizes the ordering of the elements of an array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • shuffle

      public static void shuffle(byte[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(byte[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(byte[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(byte[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(char[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(char[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(char[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(char[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(double[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(double[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(double[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(double[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(float[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(float[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(float[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(float[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(int[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(int[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(int[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(int[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(long[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(long[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(long[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(long[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(short[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
    • shuffle

      public static void shuffle(short[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static void shuffle(short[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static void shuffle(short[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static <T> void shuffle(T[] array)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Type Parameters:
      T - type of array elements
      Parameters:
      array - the array to shuffle
    • shuffle

      public static <T> void shuffle(T[] array, RandomGenerator gen)
      Randomizes the ordering of the elements of an array. All possible reorderings are equally likely.
      Type Parameters:
      T - type of array elements
      Parameters:
      array - the array to shuffle
      gen - the source of randomness
    • shuffle

      public static <T> void shuffle(T[] array, int first, int last)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Type Parameters:
      T - type of array elements
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static <T> void shuffle(T[] array, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements within a portion of an array. All possible reorderings are equally likely.
      Type Parameters:
      T - type of array elements
      Parameters:
      array - the array to shuffle
      first - the first element (inclusive) of the part of the array to shuffle
      last - the last element (exclusive) of the part of the array to shuffle
      gen - the source of randomness
      Throws:
      ArrayIndexOutOfBoundsException - if first is less than 0 or if last is greater than array.length
    • shuffle

      public static <T> void shuffle(List<T> list)
      Randomizes the ordering of the elements of a List. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Type Parameters:
      T - type of List elements
      Parameters:
      list - the List to shuffle
    • shuffle

      public static <T> void shuffle(List<T> list, RandomGenerator gen)
      Randomizes the ordering of the elements of a List. All possible reorderings are equally likely.
      Type Parameters:
      T - type of List elements
      Parameters:
      list - the List to shuffle
      gen - the source of randomness
    • shuffle

      public static <T> void shuffle(List<T> list, int first, int last)
      Randomizes the ordering of the elements of a portion of a List. All possible reorderings are equally likely.

      This method uses ThreadLocalRandom as the pseudorandom number generator, and is thus safe, and efficient (i.e., non-blocking), for use with threads.

      Type Parameters:
      T - type of List elements
      Parameters:
      list - the List to shuffle
      first - the first element (inclusive) of the part of the List to shuffle
      last - the last element (exclusive) of the part of the List to shuffle
    • shuffle

      public static <T> void shuffle(List<T> list, int first, int last, RandomGenerator gen)
      Randomizes the ordering of the elements of a portion of a List. All possible reorderings are equally likely.
      Type Parameters:
      T - type of List elements
      Parameters:
      list - the List to shuffle
      first - the first element (inclusive) of the part of the List to shuffle
      last - the last element (exclusive) of the part of the List to shuffle
      gen - the source of randomness