Class ArraySampler


  • public final class ArraySampler
    extends Object
    Utilities for shuffling an array in-place.

    Shuffles use the Fisher-Yates algorithm.

    Small ranges use batched random integer generation to increase performance.

    • Nevin Brackett-Rozinsky, Daniel Lemire, Batched Ranged Random Integer Generation, Software: Practice and Experience (to appear) arXiv:2408.06213M
    Since:
    1.6
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, boolean[] array)
      Shuffles the entries of the given array.
      static boolean[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, boolean[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static byte[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, byte[] array)
      Shuffles the entries of the given array.
      static byte[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, byte[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static char[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, char[] array)
      Shuffles the entries of the given array.
      static char[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, char[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static double[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, double[] array)
      Shuffles the entries of the given array.
      static double[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, double[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static float[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, float[] array)
      Shuffles the entries of the given array.
      static float[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, float[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static int[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, int[] array)
      Shuffles the entries of the given array.
      static int[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, int[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static long[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, long[] array)
      Shuffles the entries of the given array.
      static long[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, long[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static short[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, short[] array)
      Shuffles the entries of the given array.
      static short[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, short[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
      static <T> T[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, T[] array)
      Shuffles the entries of the given array.
      static <T> T[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng, T[] array, int from, int to)
      Shuffles the entries of the given array in the range [from, to).
    • Method Detail

      • shuffle

        public static boolean[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                        boolean[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static byte[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     byte[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static char[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     char[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static double[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                       double[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static float[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      float[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static int[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                    int[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static long[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     long[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static short[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      short[] array)
        Shuffles the entries of the given array.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static <T> T[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      T[] array)
        Shuffles the entries of the given array.
        Type Parameters:
        T - Type of the items.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        Returns:
        a reference to the given array
      • shuffle

        public static boolean[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                        boolean[] array,
                                        int from,
                                        int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static byte[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     byte[] array,
                                     int from,
                                     int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static char[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     char[] array,
                                     int from,
                                     int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static double[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                       double[] array,
                                       int from,
                                       int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static float[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      float[] array,
                                      int from,
                                      int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static int[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                    int[] array,
                                    int from,
                                    int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static long[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                     long[] array,
                                     int from,
                                     int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static short[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      short[] array,
                                      int from,
                                      int to)
        Shuffles the entries of the given array in the range [from, to).
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds
      • shuffle

        public static <T> T[] shuffle​(org.apache.commons.rng.UniformRandomProvider rng,
                                      T[] array,
                                      int from,
                                      int to)
        Shuffles the entries of the given array in the range [from, to).
        Type Parameters:
        T - Type of the items.
        Parameters:
        rng - Source of randomness.
        array - Array whose entries will be shuffled (in-place).
        from - Lower-bound (inclusive) of the sub-range.
        to - Upper-bound (exclusive) of the sub-range.
        Returns:
        a reference to the given array
        Throws:
        IndexOutOfBoundsException - if the sub-range is out of bounds