Class RandomUtils

java.lang.Object
com.blackrook.gloop.openal.struct.RandomUtils

public final class RandomUtils extends Object
Utility class for random generation.
Author:
Matthew Tropiano
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    percentChance(Random rand, int percent)
    Calculates a percent chance of something occurring.
    static int
    rand(Random rand, int x)
    Returns a random integer from 0 (inclusive) to x (exclusive).
    static int
    rand(Random rand, int base, int range)
    Returns a random integer from base to base+range.
    static boolean
    Returns a random boolean.
    static byte
    Returns a random byte value.
    static void
    randBytes(Random rand, byte[] b)
    Fills an array with random byte values.
    static char
    Returns a random character in a string.
    static double
     
    static double
    randDouble(Random rand, double lo, double hi)
    Returns a random double from lo to hi (inclusive).
    static double
    Returns a random double value from -1 to 1 (inclusive).
    static <T> T
    randElement(Random rand, T... objects)
    Returns a random entry in an array/list.
    static float
     
    static float
    randFloat(Random rand, float lo, float hi)
    Returns a random float from lo to hi (inclusive).
    static float
    Returns a random float value from -1 to 1 (inclusive).
    static double
     
    static float
    Returns a random Gaussian-distributed float value from -inf to +inf.
    static float
    randGaussFloat(Random rand, float lo, float hi)
    Returns a random Gaussian float from lo to hi (inclusive).
    static int
     
    static int
    randInt(Random rand, int lo, int hi)
    Returns a random integer from lo to hi (inclusive).
    static long
     
    static short
    randShort(Random rand, int lo, int hi)
    Returns a random short from lo to hi (inclusive).
    static short
    randShort(Random rand, short lo, short hi)
    Returns a random short from lo to hi (inclusive).
    static String
    randString(Random rand, String alphabet, int length)
    Generates a string of random characters from a provided alphabet.
    static int
    roll(Random rand, int die)
    Rolls a die.
    static int
    roll(Random rand, int n, int die)
    Rolls a die many times.

    Methods inherited from class Object

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

    • randBoolean

      public static boolean randBoolean(Random rand)
      Returns a random boolean.
      Parameters:
      rand - the random number generator.
      Returns:
      true or false.
    • randByte

      public static byte randByte(Random rand)
      Returns a random byte value.
      Parameters:
      rand - the random number generator.
      Returns:
      a value from 0 to 255.
    • randInt

      public static int randInt(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random integer.
    • randLong

      public static long randLong(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random long.
    • randFloat

      public static float randFloat(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random float value from [0 to 1) (inclusive/exclusive).
    • randDouble

      public static double randDouble(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random double value from [0 to 1) (inclusive/exclusive).
    • randGauss

      public static double randGauss(Random rand)
      Parameters:
      rand - the random number generator.
      Returns:
      a random Gaussian-distributed double value from -inf to +inf.
    • randBytes

      public static void randBytes(Random rand, byte[] b)
      Fills an array with random byte values.
      Parameters:
      rand - the random number generator.
      b - the output array to fill with bytes.
    • rand

      public static int rand(Random rand, int x)
      Returns a random integer from 0 (inclusive) to x (exclusive).
      Parameters:
      rand - the random number generator.
      x - the upper bound.
      Returns:
      the next integer.
    • rand

      public static int rand(Random rand, int base, int range)
      Returns a random integer from base to base+range.
      Parameters:
      rand - the random number generator.
      base - the lower bound.
      range - the upper bound (inclusive) of how much to add to the lower bound.
      Returns:
      the next integer.
    • randFloat

      public static float randFloat(Random rand, float lo, float hi)
      Returns a random float from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next float.
    • randGaussFloat

      public static float randGaussFloat(Random rand)
      Returns a random Gaussian-distributed float value from -inf to +inf.
      Parameters:
      rand - the random number generator.
      Returns:
      the next float.
    • randGaussFloat

      public static float randGaussFloat(Random rand, float lo, float hi)
      Returns a random Gaussian float from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next float.
    • randInt

      public static int randInt(Random rand, int lo, int hi)
      Returns a random integer from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next float.
    • randFloatN

      public static float randFloatN(Random rand)
      Returns a random float value from -1 to 1 (inclusive).
      Parameters:
      rand - the random number generator.
      Returns:
      the next float.
    • randDouble

      public static double randDouble(Random rand, double lo, double hi)
      Returns a random double from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next double.
    • randDoubleN

      public static double randDoubleN(Random rand)
      Returns a random double value from -1 to 1 (inclusive).
      Parameters:
      rand - the random number generator.
      Returns:
      the next double.
    • randShort

      public static short randShort(Random rand, int lo, int hi)
      Returns a random short from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next short.
    • randShort

      public static short randShort(Random rand, short lo, short hi)
      Returns a random short from lo to hi (inclusive).
      Parameters:
      rand - the random number generator.
      lo - the lower bound.
      hi - the upper bound.
      Returns:
      the next short.
    • randChar

      public static char randChar(Random rand, CharSequence chars)
      Returns a random character in a string.
      Parameters:
      rand - the Random instance to use.
      chars - the characters to sample.
      Returns:
      a random character.
    • randString

      public static String randString(Random rand, String alphabet, int length)
      Generates a string of random characters from a provided alphabet.
      Parameters:
      rand - the random number generator.
      alphabet - the source alphabet (cannot be null).
      length - the amount of characters to generate.
      Returns:
      a string of sampled characters.
    • randElement

      public static <T> T randElement(Random rand, T... objects)
      Returns a random entry in an array/list.
      Type Parameters:
      T - the array object type.
      Parameters:
      rand - the Random instance to use.
      objects - the array of objects to select from.
      Returns:
      a random entry from the array.
    • percentChance

      public static boolean percentChance(Random rand, int percent)
      Calculates a percent chance of something occurring.
      Parameters:
      rand - the random number generator.
      percent - the chance from 0 to 100.
      Returns:
      true if happening, false otherwise.
    • roll

      public static int roll(Random rand, int die)
      Rolls a die.
      Parameters:
      rand - the random number generator.
      die - size of the die.
      Returns:
      the outcome.
    • roll

      public static int roll(Random rand, int n, int die)
      Rolls a die many times. Example: 2d20 = roll(rand,2,20)
      Parameters:
      rand - the random number generator.
      n - times to roll.
      die - size of the die.
      Returns:
      the outcome total.