Class RandomGenerator


  • public class RandomGenerator
    extends java.lang.Object
    Utility class to help with random numbers. This class should be used instead of Math.random() and similar methods, as using class allows for "fake" random numbers that ensure deterministic behavior.
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomGenerator()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean chance​(float n)
      Produces a random float between 0.0 and 1.0, then compares that number against n and returns the result.
      float getFloat​(float min, float max)
      Returns a random float somewhere in the range between the minimum (inclusive) and maximum (exclusive).
      int getInt​(int min, int max)
      Returns a random integer somewhere in the range between the minimum (inclusive) and maximum (exclusive).
      <T> T pick​(java.util.List<T> elements)
      Picks and returns a random element from the specified list.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RandomGenerator

        public RandomGenerator()
    • Method Detail

      • getInt

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

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

        public 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 <T> T pick​(java.util.List<T> elements)
        Picks and returns a random element from the specified list.
        Throws:
        java.lang.IllegalArgumentException - if the provided list is empty.