Package com.marcpg.libpg.util
Class Randomizer
java.lang.Object
com.marcpg.libpg.util.Randomizer
Used to get random values from things
- Since:
- 0.0.1
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
All basic characters that are on a keyboard and supported in most fonts and formats. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
boolByChance
(double percentage) Returns a random boolean value based on a percentage.static boolean
boolByChance
(int numerator, int denominator) Returns a random boolean value based on a fraction represented by the numerator and denominator.static <T> T
fromArray
(T @NotNull [] in) Returns a random object from the given array.static <T> T
fromCollection
(Collection<T> in) Returns a random object from the givencollection
.static @NotNull String
generateRandomPassword
(int length) Generates a random password based onbasic characters
and a length.static @NotNull String
generateRandomPassword
(Integer length, String charset) Generates a random password based on available characters and a length.static @NotNull String
generateRandomString
(int length) Generates a random string based onbasic characters
and a length.static @NotNull String
generateRandomString
(Integer length, String charset) Generates a random string based on available characters and a length.static void
Resets the random to get true randomness.static char
Picks a random character out ofbasic characters
.static char
randomChar
(@NotNull String charset) Picks a random character out of a string.
-
Field Details
-
BASIC_CHARS
All basic characters that are on a keyboard and supported in most fonts and formats.- See Also:
-
-
Method Details
-
boolByChance
public static boolean boolByChance(int numerator, int denominator) Returns a random boolean value based on a fraction represented by the numerator and denominator. The numerator represents the desired outcomes, while the denominator represents the total possibilities.- Parameters:
numerator
- The number of desired outcomes.denominator
- The total number of possibilities.- Returns:
- Returns true or false randomly based on the fraction represented by the numerator and denominator.
-
boolByChance
public static boolean boolByChance(double percentage) Returns a random boolean value based on a percentage. A 1% chance would almost always return false. A 99% chance would almost always return true.- Parameters:
percentage
- The chance in percentage (0-100%) to return true.- Returns:
- Returns true or false randomly based on the input percentage.
-
fromCollection
Returns a random object from the givencollection
.- Type Parameters:
T
- The type of elements in the list.- Parameters:
in
- Thecollection
from which a random object should be selected.- Returns:
- A random
thing
from the inputcollection
.
-
fromArray
public static <T> T fromArray(T @NotNull [] in) Returns a random object from the given array.- Type Parameters:
T
- The type of elements in the array.- Parameters:
in
- The array from which a random object should be selected.- Returns:
- A random object from the input array.
-
generateRandomString
Generates a random string based on available characters and a length.- Parameters:
length
- The length of the string.charset
- What characters can be used in the string.- Returns:
- A randomly generated string.
-
generateRandomString
Generates a random string based onbasic characters
and a length.- Parameters:
length
- The length of the randomly generated string.- Returns:
- A randomly generated string.
-
generateRandomPassword
Generates a random password based on available characters and a length. What makes this different fromgenerateRandomString(int)
is the usage ofSecureRandom
.- Parameters:
length
- The length of the password.charset
- What characters can be used in the password.- Returns:
- A randomly generated password.
-
generateRandomPassword
Generates a random password based onbasic characters
and a length. What makes this different fromgenerateRandomString(int)
is the usage ofSecureRandom
.- Parameters:
length
- The length of the password.- Returns:
- A randomly generated password.
-
randomChar
Picks a random character out of a string.- Parameters:
charset
- All available characters, can have one character multiple times.- Returns:
- A random character.
-
randomChar
public static char randomChar()Picks a random character out ofbasic characters
.- Returns:
- A random character.
-
newRandom
public static void newRandom()Resets the random to get true randomness.
-