Random class.
Use secure() to get the singleton instance based on SecureRandom#SecureRandom() which uses a secure random number generator implementing the
default random number algorithm.
Use secureStrong() to get the singleton instance based on SecureRandom#getInstanceStrong() which uses an instance that was selected by using
the algorithms/providers specified in the securerandom.strongAlgorithms Security property.
Use insecure() to get the singleton instance based on ThreadLocalRandom#current() which is not cryptographically secure. In addition,
instances do not use a cryptographically random seed unless the system property java.util.secureRandomSeed is set to
true.
Starting in version 3.17.0, the method secure() uses SecureRandom#SecureRandom() instead of SecureRandom#getInstanceStrong(), and
adds secureStrong().
Starting in version 3.16.0, this class uses secure() for static methods and adds insecure().
Starting in version 3.15.0, this class uses SecureRandom#getInstanceStrong() for static methods.
Before version 3.15.0, this class used ThreadLocalRandom#current() for static methods, which is not cryptographically secure.
Please note that the Apache Commons project provides a component dedicated to pseudo-random number generation, namely Commons RNG, that may be a better choice for applications with more stringent requirements (performance and/or correctness).
secure(), secureStrong(), insecure(), SecureRandom#SecureRandom(), SecureRandom#getInstanceStrong(), ThreadLocalRandom#current(), RandomStringUtils
| Modifier and Type | Field and Description |
|---|---|
| private static RandomUtils | |
| private final Supplier | |
| private static RandomUtils | |
| private static final ThreadLocal | |
| private static RandomUtils | |
| private static final Supplier |
| Access | Constructor and Description |
|---|---|
| public | RandomUtils()
Deprecated
TODO Make private in 4.0.
|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static RandomUtils | Returns: the singleton instance based onThreadLocalRandom#current().Gets the singleton instance based on |
| public static boolean | Returns: the random boolean.
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random boolean value. |
| public static byte[] | Returns: the random byte array.the size of the returned array. count)
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates an array of random bytes. |
| public static double | Returns: the random double.
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive). |
| public static double | Returns: the random double.the smallest value that can be returned, must be non-negative. startInclusive, final double the upper bound (not included). endExclusive)
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random double within the specified range. |
| public static float | Returns: the random float.
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive). |
| public static float | Returns: the random floatthe smallest value that can be returned, must be non-negative. startInclusive, final float the upper bound (not included). endExclusive)
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random float within the specified range. |
| public static int | Returns: the random integer.
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive). |
| public static int | Returns: the random integer.the smallest value that can be returned, must be non-negative. startInclusive, final int the upper bound (not included). endExclusive)
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random integer within the specified range. |
| public static long | Returns: the random long.
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive). |
| public static long | Returns: the random long.the smallest value that can be returned, must be non-negative. startInclusive, final long the upper bound (not included). endExclusive)
Deprecated
Use
secure(), secureStrong(), or insecure().
Generates a random long within the specified range. |
| pack-priv Random | |
| public boolean | |
| public byte[] | Returns: the random byte array.the size of the returned array. count)Generates an array of random bytes. |
| public double | Returns: the random double.Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive). |
| public double | Returns: the random doublethe smallest value that can be returned, must be non-negative. startInclusive, final double the upper bound (not included). endExclusive)Generates a random double within the specified range. |
| public float | Returns: the random float.Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive). |
| public float | Returns: the random float.the smallest value that can be returned, must be non-negative. startInclusive, final float the upper bound (not included). endExclusive)Generates a random float within the specified range. |
| public int | Returns: the random integer.Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive). |
| public int | Returns: the random integer.the smallest value that can be returned, must be non-negative. startInclusive, final int the upper bound (not included). endExclusive)Generates a random integer within the specified range. |
| public long | Returns: the random long.Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive). |
| private long | Returns: a randomlong value between 0 (inclusive) and n (exclusive).Bound on the random number to be returned. Must be positive. n)Generates a |
| public long | Returns: the random long.the smallest value that can be returned, must be non-negative. startInclusive, final long the upper bound (not included). endExclusive)Generates a random long within the specified range. |
| public static RandomUtils | Returns: the singleton instance based onSecureRandom#SecureRandom().Gets the singleton instance based on |
| pack-priv static SecureRandom | |
| public static RandomUtils | Returns: the singleton instance based onSecureRandom#getInstanceStrong().Gets the singleton instance based on |
| public String |
| INSECURE | back to summary |
|---|---|
| private static RandomUtils INSECURE | |
| random | back to summary |
|---|---|
| private final Supplier<Random> random | |
| SECURE | back to summary |
|---|---|
| private static RandomUtils SECURE | |
| SECURE_RANDOM_STRONG | back to summary |
|---|---|
| private static final ThreadLocal<SecureRandom> SECURE_RANDOM_STRONG | |
| SECURE_STRONG | back to summary |
|---|---|
| private static RandomUtils SECURE_STRONG | |
| SECURE_STRONG_SUPPLIER | back to summary |
|---|---|
| private static final Supplier<Random> SECURE_STRONG_SUPPLIER | |
| RandomUtils | back to summary |
|---|---|
| public RandomUtils()
Deprecated TODO Make private in 4.0.
This constructor is public to permit tools that require a JavaBean instance to operate.
| |
| RandomUtils | back to summary |
|---|---|
| private RandomUtils(final Supplier<Random> random) | |
| insecure | back to summary |
|---|---|
| public static RandomUtils insecure() Gets the singleton instance based on
The method
| |
| nextBoolean | back to summary |
|---|---|
| public static boolean nextBoolean()
Deprecated Use Generates a random boolean value.
| |
| nextBytes | back to summary |
|---|---|
| public static byte[] nextBytes(final int count)
Deprecated Use Generates an array of random bytes.
| |
| nextDouble | back to summary |
|---|---|
| public static double nextDouble()
Deprecated Use Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).
| |
| nextDouble | back to summary |
|---|---|
| public static double nextDouble(final double startInclusive, final double endExclusive)
Deprecated Use Generates a random double within the specified range.
| |
| nextFloat | back to summary |
|---|---|
| public static float nextFloat()
Deprecated Use Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).
| |
| nextFloat | back to summary |
|---|---|
| public static float nextFloat(final float startInclusive, final float endExclusive)
Deprecated Use Generates a random float within the specified range.
| |
| nextInt | back to summary |
|---|---|
| public static int nextInt()
Deprecated Use Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).
| |
| nextInt | back to summary |
|---|---|
| public static int nextInt(final int startInclusive, final int endExclusive)
Deprecated Use Generates a random integer within the specified range.
| |
| nextLong | back to summary |
|---|---|
| public static long nextLong()
Deprecated Use Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).
| |
| nextLong | back to summary |
|---|---|
| public static long nextLong(final long startInclusive, final long endExclusive)
Deprecated Use Generates a random long within the specified range.
| |
| random | back to summary |
|---|---|
| pack-priv Random random() | |
| randomBoolean | back to summary |
|---|---|
| public boolean randomBoolean() Generates a random boolean value.
| |
| randomBytes | back to summary |
|---|---|
| public byte[] randomBytes(final int count) Generates an array of random bytes.
| |
| randomDouble | back to summary |
|---|---|
| public double randomDouble() Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).
| |
| randomDouble | back to summary |
|---|---|
| public double randomDouble(final double startInclusive, final double endExclusive) Generates a random double within the specified range.
| |
| randomFloat | back to summary |
|---|---|
| public float randomFloat() Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).
| |
| randomFloat | back to summary |
|---|---|
| public float randomFloat(final float startInclusive, final float endExclusive) Generates a random float within the specified range.
| |
| randomInt | back to summary |
|---|---|
| public int randomInt() Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).
| |
| randomInt | back to summary |
|---|---|
| public int randomInt(final int startInclusive, final int endExclusive) Generates a random integer within the specified range.
| |
| randomLong | back to summary |
|---|---|
| public long randomLong() Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).
| |
| randomLong | back to summary |
|---|---|
| private long randomLong(final long n) Generates a
| |
| randomLong | back to summary |
|---|---|
| public long randomLong(final long startInclusive, final long endExclusive) Generates a random long within the specified range.
| |
| secure | back to summary |
|---|---|
| public static RandomUtils secure() Gets the singleton instance based on
The method
| |
| secureRandom | back to summary |
|---|---|
| pack-priv static SecureRandom secureRandom() | |
| secureStrong | back to summary |
|---|---|
| public static RandomUtils secureStrong() Gets the singleton instance based on
The method
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |