Package host.anzo.commons.utils
Class Rnd
java.lang.Object
host.anzo.commons.utils.Rnd
Utility class for generating random numbers and strings.
Provides various methods to generate random values of different types.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleget()Gets a random number from 0 (inclusive) to 1 (exclusive).static doubleget(double n) Gets a random double number between 0 (inclusive) and n (exclusive).static doubleget(double min, double max) Gets a random double number between min (inclusive) and max (exclusive).static floatget(float n) Gets a random float number between 0 (inclusive) and n (exclusive).static floatget(float min, float max) Gets a random float number between min (inclusive) and max (exclusive).static intget(int n) Gets a random integer between 0 (inclusive) and n (exclusive).static intget(int @NotNull [] list) Gets a random integer from the provided integer array.static intget(int min, int max) Gets a random integer between min (inclusive) and max (inclusive).static longget(long n) Gets a random long number between 0 (inclusive) and n (exclusive).static longget(long min, long max) Gets a random long number between min (inclusive) and max (inclusive).static <E> Eget(E @NotNull [] list) Gets a random element from the provided array.static <E extends Enum<E>>
EGets a random element from the provided enum class.static <E> Eget(@NotNull Collection<E> collection) Gets a random element from the provided collection.static <E> EGets a random element from the provided list.static <E> EGets a random element from the provided list or returns a default value if the list is empty.static booleangetChance(double chance) Randomizer for chance calculation.static booleangetChance(int chance) Randomizer for chance calculation.static booleangetChance(int chance, int divider) Randomizer for chance calculation with a divider.static @NotNull StringgetName()Retrieves a random human-readablestatic doublegetRandomWithTick(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> getRouletteEntries(List<RouletteRandomEntry> input, int count) Gets a list of random entries from a roulette selection.static @Nullable RouletteRandomEntrygetRouletteEntry(List<RouletteRandomEntry> input) Gets a single random entry from a roulette selection.static @NotNull StringgetString(int length) Generates a random string of alphabetic characters of a specified length.static @NotNull StringgetStringHex(int length) Generates a random string of hexadecimal characters of a specified length.static booleanGets a random boolean value.static byte[]nextBytes(byte[] bytes) Fills the provided byte array with random bytes.static doubleGets a random double from the full range of double values.static doubleGets a random number from a Gaussian distribution.static intnextInt()Gets a random integer from the full range of int values.
-
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:
trueif 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:
trueif 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 100divider- the divider to adjust the chance- Returns:
trueif 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
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
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 fromdefaultValue- the value to return if the list is empty- Returns:
- a random element from the list or the default value
-
get
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
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 fromcount- 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 randomnessrandomSeed- the seed to influence randomness- Returns:
- a random double value between min and max
-
getString
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
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
Retrieves a random human-readable- Returns:
- a randomly generated name
-