Class RandomGenerator

java.lang.Object
nl.colorize.multimedialib.math.RandomGenerator

public class RandomGenerator extends Object
Utility class to help with random numbers. This class uses a shared global Random instance. This prevents situations where application code creates Random instances across various locations, making it easier to toggle between "real" random and deterministic pseudo-random.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    chance(float n)
    Produces a random float between 0.0 and 1.0, then compares that number against n and returns the result.
    static float
    getFloat(float min, float max)
    Returns a random float somewhere in the range between the minimum (inclusive) and maximum (exclusive).
    static int
    getInt(int min, int max)
    Returns a random integer somewhere in the range between the minimum (inclusive) and maximum (exclusive).
    static <T> T
    pick(Iterable<T> elements)
    Picks and returns a random element from the specified iterator.
    static <T> T
    pick(List<T> elements)
    Picks and returns a random element from the specified list.
    static <T> T
    pick(Map<T,Integer> choices)
    Picks a random key from the specified map.
    static <T> T
    pick(Set<T> elements)
    Picks and returns a random element from the specified set.
    static <T> T
    pick(Stream<T> elements)
    Picks and returns a random element from the specified stream.
    static Point2D
    pickPoint(Rect bounds)
    Picks a random point somewhere within the specified rectangle.
    static void
    Changes the random number generator used by this class to generate "true" random numbers.
    static void
    seed(long value)
    Changes the random number generator used by this class to generate deterministic pseudo-random numbers based on the specified seed value.
    static <T> List<T>
    shuffle(List<T> original)
    Returns a new list that contains the same elements as the original, but with all elements shuffled to be in random order.

    Methods inherited from class java.lang.Object

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

    • seed

      public static void seed(long value)
      Changes the random number generator used by this class to generate deterministic pseudo-random numbers based on the specified seed value.
    • randomSeed

      public static void randomSeed()
      Changes the random number generator used by this class to generate "true" random numbers. This method can be used to revert the changes made by using seed(long).
    • getInt

      public static int getInt(int min, int max)
      Returns a random integer somewhere in the range between the minimum (inclusive) and maximum (exclusive).
    • getFloat

      public static float getFloat(float min, float max)
      Returns a random float somewhere in the range between the minimum (inclusive) and maximum (exclusive).
    • chance

      public static boolean chance(float n)
      Produces a random float between 0.0 and 1.0, then compares that number against n and returns the result. In other words, passing a value of 0.9 against this method will have a 90% chance of returning true.
    • pick

      public static <T> T pick(List<T> elements)
      Picks and returns a random element from the specified list.
      Throws:
      IllegalArgumentException - if the provided list is empty.
    • pick

      public static <T> T pick(Set<T> elements)
      Picks and returns a random element from the specified set.
      Throws:
      IllegalArgumentException - if the provided set is empty.
    • pick

      public static <T> T pick(Iterable<T> elements)
      Picks and returns a random element from the specified iterator.
      Throws:
      IllegalArgumentException - if the provided iterator is empty.
    • pick

      public static <T> T pick(Stream<T> elements)
      Picks and returns a random element from the specified stream.
      Throws:
      IllegalArgumentException - if the provided stream is empty.
    • pick

      public static <T> T pick(Map<T,Integer> choices)
      Picks a random key from the specified map. The chance that one of the elements will be chosen is indicated by the values in the map. So if two keys have the same value, the chance that they will be picked is equally likely. A value of 0 indicates the value will never be chosen.
      Throws:
      IllegalArgumentException - if the provided map is empty.
    • shuffle

      public static <T> List<T> shuffle(List<T> original)
      Returns a new list that contains the same elements as the original, but with all elements shuffled to be in random order. This method is similar to Collections.shuffle(List), but creates a new list instead of trying to modify the original one.
    • pickPoint

      public static Point2D pickPoint(Rect bounds)
      Picks a random point somewhere within the specified rectangle.