Class RandomExtra

java.lang.Object
com.hypixel.hytale.math.random.RandomExtra

public final class RandomExtra extends Object
  • Method Details

    • randomBinomial

      public static double randomBinomial()
    • randomRange

      public static double randomRange(@Nonnull double[] range)
    • randomRange

      public static double randomRange(double from, double to)
    • randomRange

      public static float randomRange(@Nonnull float[] range)
    • randomRange

      public static float randomRange(float from, float to)
    • randomRange

      public static int randomRange(int bound)
    • randomRange

      public static int randomRange(@Nonnull int[] range)
    • randomRange

      public static int randomRange(int from, int to)
    • randomRange

      public static long randomRange(long from, long to)
    • randomDuration

      public static Duration randomDuration(@Nonnull Duration from, @Nonnull Duration to)
    • randomBoolean

      public static boolean randomBoolean()
    • randomElement

      public static <T> T randomElement(@Nonnull List<T> collection)
    • jitter

      @Nonnull public static org.joml.Vector3d jitter(@Nonnull org.joml.Vector3d vec, double maxRange)
    • randomWeightedElement

      @Nullable public static <T> T randomWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull ToDoubleFunction<T> weight)
      Pick a random element from a collection of weighted elements.
      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      weight - function returning weight of element
      Returns:
      randomly chosen element
    • randomWeightedElement

      @Nullable public static <T> T randomWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull ToDoubleFunction<T> weight, double sumWeights)
      Pick a random element from a collection of weighted elements using a known fixed sum of weights.

      Use this function when sum of weights is known.

      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      weight - function returning weight of element
      sumWeights - sum of weights
      Returns:
      randomly chosen element
    • randomIntWeightedElement

      @Nullable public static <T> T randomIntWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull ToIntFunction<T> weight)
      Pick a random element from a collection of weighted elements.
      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      weight - function returning weight of element
      Returns:
      randomly chosen element
    • randomIntWeightedElement

      @Nullable public static <T> T randomIntWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull ToIntFunction<T> weight, int sumWeights)
      Pick a random element from a collection of weighted elements using a known fixed sum of weights.

      Use this function when sum of weights is known.

      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      weight - function returning weight of element
      sumWeights - sum of weights
      Returns:
      randomly chosen element
    • randomWeightedElementFiltered

      @Nullable public static <T> T randomWeightedElementFiltered(@Nonnull Collection<? extends T> elements, @Nonnull Predicate<T> filter, @Nonnull ToIntFunction<T> weight)
      Pick a random element from a collection of weighted elements filtering unwanted elements.
      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      filter - predicate return false for elements to ignore
      weight - function returning weight of element
      Returns:
      randomly chosen element
    • randomWeightedElementFiltered

      @Nullable public static <T> T randomWeightedElementFiltered(@Nonnull Collection<? extends T> elements, @Nonnull Predicate<T> filter, @Nonnull ToIntFunction<T> weight, int sumWeights)
      Pick a random element from a collection of weighted elements filtering unwanted elements using a known fixed sum of weights.

      Use this function when sum of weights is known.

      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      filter - predicate return false for elements to ignore
      weight - function returning weight of element
      sumWeights - sum of weights
      Returns:
      randomly chosen element
    • randomWeightedElement

      @Nullable public static <T> T randomWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull Predicate<T> filter, @Nonnull ToDoubleFunction<T> weight)
      Pick a random element from a collection of weighted elements filtering unwanted elements.
      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      filter - predicate return false for elements to ignore
      weight - function returning weight of element
      Returns:
      randomly chosen element
    • randomWeightedElement

      @Nullable public static <T> T randomWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull Predicate<T> filter, @Nonnull ToDoubleFunction<T> weight, double sumWeights)
      Pick a random element from a collection of weighted elements filtering unwanted elements using a known fixed sum of weights.

      Use this function when sum of weights is known.

      Type Parameters:
      T - type of element
      Parameters:
      elements - the collection of elements
      filter - predicate return false for elements to ignore
      weight - function returning weight of element
      sumWeights - sum of weights
      Returns:
      randomly chosen element
    • randomWeightedElement

      @Nullable public static <T,U> T randomWeightedElement(@Nonnull Collection<? extends T> elements, @Nonnull BiPredicate<T,U> filter, @Nonnull ToDoubleBiFunction<T,U> weight, double sumWeights, U meta)
    • reservoirSample

      public static <T> void reservoirSample(@Nonnull List<T> input, @Nonnull Predicate<T> matcher, int count, @Nonnull List<T> picked)
      Pick a random sample from a collection of elements where the actual number of matching elements is unknown.

      An implementation of Reservoir Sampling.

      Type Parameters:
      T - type of element
      Parameters:
      input - the list of elements to sample from
      matcher - the predicate for matching elements that are valid picks
      count - the number of elements to pick
      picked - an output list for placing the elements chosen in the sampling
    • reservoirSample

      public static <E, S extends List<E>, F, T extends List<F>, G, H> void reservoirSample(@Nonnull S input, @Nonnull TriFunction<E,G,H,F> filter, int count, @Nonnull T picked, G g, H h)
      Pick a random sample from a collection of elements where the actual number of matching elements is unknown.

      An implementation of Reservoir Sampling.

      Parameters:
      input - the list of elements to sample from
      filter - function accepting element from input list and return element for output list or null if element should not be considered
      count - the number of elements to pick
      picked - an output list for placing the elements chosen in the sampling
      g - user provided object passed to #filter
      h - second user provided object passed to #filter
    • reservoirSample

      public static <E, T extends List<E>> void reservoirSample(E element, int count, @Nonnull T picked)
      Pick a random sample from a collection of elements where the actual number of matching elements is unknown.

      An implementation of Reservoir Sampling.

      Parameters:
      element - element from input set to sample from
      count - the number of elements to pick
      picked - an output list for placing the elements chosen in the sampling
    • reservoirSample

      public static int reservoirSample(int n, int count, @Nonnull int[] out)
      Pick a random sample of indices from a fixed-size index space, writing the result into a reused buffer.

      An implementation of Reservoir Sampling.

      Parameters:
      n - the size of the index space to sample from
      count - the number of indices to pick
      out - an output buffer for placing the indices chosen in the sampling
      Returns:
      the number of indices written, min(count, n)
    • pickWeightedIndex

      public static int pickWeightedIndex(@Nonnull double[] weights)
      Pick a random weighted index based on an array of weights
      Parameters:
      weights - an array of weights
      Returns:
      a random index picked based on these weights