Class RandomUtil


  • public final class RandomUtil
    extends Object
    • Method Detail

      • choose

        public static <T> @Nullable T choose​(Random rand,
                                             T[] array)
      • distinctIntegers

        public static int[] distinctIntegers​(Random rand,
                                             int num,
                                             int min,
                                             int max)
        Sample a specified number of distinct integers from a specified range.

        The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each integer within in the specified range to appear in the returned array but no equal probability of their order.

        Parameters:
        rand - the random instance for generating numbers
        num - number of integers to sample
        min - lower bound (inclusive) of sampled values
        max - upper bound (exclusive) of samples values
        Returns:
        an array of distinct integers sampled from the specified range
      • distinctIntegers

        public static int[] distinctIntegers​(Random rand,
                                             int num,
                                             int max)
      • sample

        public static <T> List<T> sample​(Random rand,
                                         List<? extends T> list,
                                         int num)
      • sampleUnique

        public static <T> List<T> sampleUnique​(Random rand,
                                               List<? extends T> list,
                                               int num)
        Sample a specified number of elements from specified list.

        The implementation is based on Floyd's Algorithm F2. Note that this algorithm ensures equal probability of each element within in the specified list to appear in the returned list but no equal probability of their order.

        Parameters:
        rand - the random instance for generating numbers
        list - the list to sample elements from
        num - number of integers to sample
        Returns:
        a list of distinct elements sampled from the specified list