Class Rnd

java.lang.Object
host.anzo.commons.utils.Rnd

public class Rnd extends Object
Utility class for generating random numbers and strings. Provides various methods to generate random values of different types.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rnd()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    get()
    Gets a random number from 0 (inclusive) to 1 (exclusive).
    static double
    get(double n)
    Gets a random double number between 0 (inclusive) and n (exclusive).
    static double
    get(double min, double max)
    Gets a random double number between min (inclusive) and max (exclusive).
    static float
    get(float n)
    Gets a random float number between 0 (inclusive) and n (exclusive).
    static float
    get(float min, float max)
    Gets a random float number between min (inclusive) and max (exclusive).
    static int
    get(int n)
    Gets a random integer between 0 (inclusive) and n (exclusive).
    static int
    get(int @NotNull [] list)
    Gets a random integer from the provided integer array.
    static int
    get(int min, int max)
    Gets a random integer between min (inclusive) and max (inclusive).
    static long
    get(long n)
    Gets a random long number between 0 (inclusive) and n (exclusive).
    static long
    get(long min, long max)
    Gets a random long number between min (inclusive) and max (inclusive).
    static <E> E
    get(E @NotNull [] list)
    Gets a random element from the provided array.
    static <E extends Enum<E>>
    E
    get(@NotNull Class<E> clazz)
    Gets a random element from the provided enum class.
    static <E> E
    get(@NotNull Collection<E> collection)
    Gets a random element from the provided collection.
    static <E> E
    get(@NotNull List<E> list)
    Gets a random element from the provided list.
    static <E> E
    get(@NotNull List<E> list, E defaultValue)
    Gets a random element from the provided list or returns a default value if the list is empty.
    static boolean
    getChance(double chance)
    Randomizer for chance calculation.
    static boolean
    getChance(int chance)
    Randomizer for chance calculation.
    static boolean
    getChance(int chance, int divider)
    Randomizer for chance calculation with a divider.
    static @NotNull String
    Retrieves a random human-readable
    static double
    getRandomWithTick(double min, double max, long serverTick, long randomSeed)
    Generates a random number within a specified range, influenced by server tick and random seed.
    static @NotNull List<RouletteRandomEntry>
    Gets a list of random entries from a roulette selection.
    static @Nullable RouletteRandomEntry
    Gets a single random entry from a roulette selection.
    static @NotNull String
    getString(int length)
    Generates a random string of alphabetic characters of a specified length.
    static @NotNull String
    getStringHex(int length)
    Generates a random string of hexadecimal characters of a specified length.
    static boolean
    Gets a random boolean value.
    static byte[]
    nextBytes(byte[] bytes)
    Fills the provided byte array with random bytes.
    static double
    Gets a random double from the full range of double values.
    static double
    Gets a random number from a Gaussian distribution.
    static int
    Gets a random integer from the full range of int values.

    Methods inherited from class java.lang.Object

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

    • Rnd

      public Rnd()
  • Method Details

    • get

      public static double get()
      Gets a random number from 0 (inclusive) to 1 (exclusive).
      Returns:
      a random double value between 0.0 and 1.0
    • get

      public static int get(int n)
      Gets a random integer between 0 (inclusive) and n (exclusive).
      Parameters:
      n - The superior limit (exclusive)
      Returns:
      if positive: A number from 0 to n-1, if negative A number from n-1 to 0
    • get

      public static long get(long n)
      Gets a random long number between 0 (inclusive) and n (exclusive).
      Parameters:
      n - The superior limit (exclusive)
      Returns:
      if positive: A number from 0 to n-1, if negative A number from n-1 to 0
    • get

      public static double get(double n)
      Gets a random double number between 0 (inclusive) and n (exclusive).
      Parameters:
      n - The superior limit (exclusive)
      Returns:
      if positive: A number from 0 to n-1, if negative A number from n-1 to 0
    • get

      public static float get(float n)
      Gets a random float number between 0 (inclusive) and n (exclusive).
      Parameters:
      n - The superior limit (exclusive)
      Returns:
      if positive: A number from 0 to n-1, if negative A number from n-1 to 0
    • get

      public static int get(int min, int max)
      Gets a random integer between min (inclusive) and max (inclusive).
      Parameters:
      min - The lower limit (inclusive)
      max - The upper limit (inclusive)
      Returns:
      a random integer between min and max
    • get

      public static long get(long min, long max)
      Gets a random long number between min (inclusive) and max (inclusive).
      Parameters:
      min - The lower limit (inclusive)
      max - The upper limit (inclusive)
      Returns:
      a random long number between min and max
    • get

      public static float get(float min, float max)
      Gets a random float number between min (inclusive) and max (exclusive).
      Parameters:
      min - The lower limit (inclusive)
      max - The upper limit (exclusive)
      Returns:
      a random float number between min and max
    • get

      public static double get(double min, double max)
      Gets a random double number between min (inclusive) and max (exclusive).
      Parameters:
      min - The lower limit (inclusive)
      max - The upper limit (exclusive)
      Returns:
      a random double number between min and max
    • nextInt

      public static int nextInt()
      Gets a random integer from the full range of int values.
      Returns:
      a random integer
    • nextDouble

      public static double nextDouble()
      Gets a random double from the full range of double values.
      Returns:
      a random double
    • nextGaussian

      public static double nextGaussian()
      Gets a random number from a Gaussian distribution.
      Returns:
      a random double from a Gaussian distribution
    • nextBoolean

      public static boolean nextBoolean()
      Gets a random boolean value.
      Returns:
      a random boolean
    • nextBytes

      public static byte[] nextBytes(byte[] bytes)
      Fills the provided byte array with random bytes.
      Parameters:
      bytes - the byte array to fill
      Returns:
      the filled byte array
    • getChance

      public static boolean getChance(int chance)
      Randomizer for chance calculation. Recommending to use instead Rnd.get(n, n).
      Parameters:
      chance - in percent from 0 to 100
      Returns:
      true if success If chance <= 0, return false If chance >= 100, return true
    • getChance

      public static boolean getChance(double chance)
      Randomizer for chance calculation. Recommending to use instead Rnd.get(n, n) if we need high precision values.
      Parameters:
      chance - in percent from 0 to 100
      Returns:
      true if success If chance <= 0, return false If chance >= 100, return true
    • getChance

      public static boolean getChance(int chance, int divider)
      Randomizer for chance calculation with a divider.
      Parameters:
      chance - in percent from 0 to 100
      divider - the divider to adjust the chance
      Returns:
      true if success
    • get

      public static <E> E get(E @NotNull [] list)
      Gets a random element from the provided array.
      Type Parameters:
      E - the type of elements in the array
      Parameters:
      list - the array to select from
      Returns:
      a random element from the array
    • get

      public static int get(int @NotNull [] list)
      Gets a random integer from the provided integer array.
      Parameters:
      list - the integer array to select from
      Returns:
      a random integer from the array
    • get

      @Nullable public static <E> E get(@NotNull @NotNull List<E> list)
      Gets a random element from the provided list.
      Type Parameters:
      E - the type of elements in the list
      Parameters:
      list - the list to select from
      Returns:
      a random element from the list, or null if the list is empty
    • get

      public static <E> E get(@NotNull @NotNull List<E> list, E defaultValue)
      Gets a random element from the provided list or returns a default value if the list is empty.
      Type Parameters:
      E - the type of elements in the list
      Parameters:
      list - the list to select from
      defaultValue - the value to return if the list is empty
      Returns:
      a random element from the list or the default value
    • get

      @Nullable public static <E> E get(@NotNull @NotNull Collection<E> collection)
      Gets a random element from the provided collection.
      Type Parameters:
      E - the type of elements in the collection
      Parameters:
      collection - the collection to select from
      Returns:
      a random element from the collection, or null if the collection is empty
    • get

      public static <E extends Enum<E>> E get(@NotNull @NotNull Class<E> clazz)
      Gets a random element from the provided enum class.
      Type Parameters:
      E - the type of the enum
      Parameters:
      clazz - the class of the enum to select from
      Returns:
      a random enum constant from the specified class
    • getRouletteEntries

      @NotNull public static @NotNull List<RouletteRandomEntry> getRouletteEntries(List<RouletteRandomEntry> input, int count)
      Gets a list of random entries from a roulette selection.
      Parameters:
      input - the list of entries to select from
      count - the number of entries to select
      Returns:
      a list of randomly selected RouletteRandomEntry objects
    • getRouletteEntry

      @Nullable public static @Nullable RouletteRandomEntry getRouletteEntry(List<RouletteRandomEntry> input)
      Gets a single random entry from a roulette selection.
      Parameters:
      input - the list of entries to select from
      Returns:
      a randomly selected RouletteRandomEntry object, or null if the input list is empty
    • getRandomWithTick

      public static double getRandomWithTick(double min, double max, long serverTick, long randomSeed)
      Generates a random number within a specified range, influenced by server tick and random seed.
      Parameters:
      min - the minimum value (inclusive)
      max - the maximum value (exclusive)
      serverTick - the server tick to influence randomness
      randomSeed - the seed to influence randomness
      Returns:
      a random double value between min and max
    • getString

      @NotNull public static @NotNull String getString(int length)
      Generates a random string of alphabetic characters of a specified length.
      Parameters:
      length - the length of the random string to generate
      Returns:
      a random alphabetic string of the specified length
    • getStringHex

      @NotNull public static @NotNull String getStringHex(int length)
      Generates a random string of hexadecimal characters of a specified length.
      Parameters:
      length - the length of the random hexadecimal string to generate
      Returns:
      a random hexadecimal string of the specified length
    • getName

      @NotNull public static @NotNull String getName()
      Retrieves a random human-readable
      Returns:
      a randomly generated name