Class RandomUtil

java.lang.Object
ru.progrm_jarvis.javacommons.random.RandomUtil

public final class RandomUtil extends Object
Utility for getting random values.
  • Method Details

    • randomSign

      public static int randomSign()
      Provides 1 or -1 randomly.
      Returns:
      1 or -1
    • getRandom

      public static <T> T getRandom(@NonNull @NonNull Map<T,Integer> chancedValues)
      Gets a random value from the map specified considering chances.
      Type Parameters:
      T - type of value randomly got
      Parameters:
      chancedValues - values from which to get a random one the keys of the map are the actual values, the values of the map are the chances
      Returns:
      got random value
      Throws:
      IllegalArgumentException - if values is empty
    • getRandom

      public static <T> T getRandom(@NonNull @NonNull List<T> values)
      Gets a random value from the list specified.
      Type Parameters:
      T - type of value randomly got
      Parameters:
      values - values from which to get a random one
      Returns:
      got random value
      Throws:
      IllegalArgumentException - if values is empty
    • getRandom

      public static <T> T getRandom(@NonNull @NonNull Collection<T> values)
      Gets a random value from the collection specified.
      Type Parameters:
      T - type of value randomly got
      Parameters:
      values - values from which to get a random one
      Returns:
      got random value
      Throws:
      IllegalArgumentException - if values is empty