public class RandomUtils extends Object
Random class.
Use secure() to get the singleton instance based on 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() 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.getInstanceStrong(),
ThreadLocalRandom.current(),
RandomStringUtils| Constructor and Description |
|---|
RandomUtils()
Deprecated.
TODO Make private in 4.0.
|
| Modifier and Type | Method and Description |
|---|---|
static RandomUtils |
insecure()
Gets the singleton instance based on
ThreadLocalRandom.current(); which is not cryptographically
secure; use secure() to use an algorithms/providers specified in the
securerandom.strongAlgorithms Security property. |
static boolean |
nextBoolean()
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static byte[] |
nextBytes(int count)
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static double |
nextDouble()
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static double |
nextDouble(double startInclusive,
double endExclusive)
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static float |
nextFloat()
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static float |
nextFloat(float startInclusive,
float endExclusive)
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static int |
nextInt()
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static int |
nextInt(int startInclusive,
int endExclusive)
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static long |
nextLong()
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
static long |
nextLong(long startInclusive,
long endExclusive)
Deprecated.
Use
secure(), secureStrong(),or insecure(). |
boolean |
randomBoolean()
Generates a random boolean value.
|
byte[] |
randomBytes(int count)
Generates an array of random bytes.
|
double |
randomDouble()
Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).
|
double |
randomDouble(double startInclusive,
double endExclusive)
Generates a random double within the specified range.
|
float |
randomFloat()
Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).
|
float |
randomFloat(float startInclusive,
float endExclusive)
Generates a random float within the specified range.
|
int |
randomInt()
Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).
|
int |
randomInt(int startInclusive,
int endExclusive)
Generates a random integer within the specified range.
|
long |
randomLong()
Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).
|
long |
randomLong(long startInclusive,
long endExclusive)
Generates a random long within the specified range.
|
static RandomUtils |
secure()
Gets the singleton instance based on
SecureRandom() which uses an algorithms/providers
specified in the securerandom.strongAlgorithms Security property. |
static RandomUtils |
secureStrong()
Gets the singleton instance based on
SecureRandom.getInstanceStrong() which uses an algorithms/providers
specified in the securerandom.strongAlgorithms Security property. |
String |
toString() |
@Deprecated public RandomUtils()
RandomUtils instances should NOT be constructed in standard programming. Instead, the class should be
used as RandomUtils.nextBytes(5);.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static RandomUtils insecure()
ThreadLocalRandom.current(); which is not cryptographically
secure; use secure() to use an algorithms/providers specified in the
securerandom.strongAlgorithms Security property.
The method ThreadLocalRandom.current() is called on-demand.
ThreadLocalRandom.current().ThreadLocalRandom.current(),
secure()@Deprecated public static boolean nextBoolean()
secure(), secureStrong(),or insecure().@Deprecated public static byte[] nextBytes(int count)
secure(), secureStrong(),or insecure().count - the size of the returned arrayIllegalArgumentException - if count is negative@Deprecated public static double nextDouble()
secure(), secureStrong(),or insecure().nextDouble(double, double)@Deprecated public static double nextDouble(double startInclusive, double endExclusive)
secure(), secureStrong(),or insecure().startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negative@Deprecated public static float nextFloat()
secure(), secureStrong(),or insecure().nextFloat(float, float)@Deprecated public static float nextFloat(float startInclusive, float endExclusive)
secure(), secureStrong(),or insecure().startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negative@Deprecated public static int nextInt()
secure(), secureStrong(),or insecure().nextInt(int, int)@Deprecated public static int nextInt(int startInclusive, int endExclusive)
secure(), secureStrong(),or insecure().startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negative@Deprecated public static long nextLong()
secure(), secureStrong(),or insecure().nextLong(long, long)@Deprecated public static long nextLong(long startInclusive, long endExclusive)
secure(), secureStrong(),or insecure().startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negativepublic static RandomUtils secure()
SecureRandom() which uses an algorithms/providers
specified in the securerandom.strongAlgorithms Security property.
The method SecureRandom() is called on-demand.
SecureRandom().SecureRandom()public static RandomUtils secureStrong()
SecureRandom.getInstanceStrong() which uses an algorithms/providers
specified in the securerandom.strongAlgorithms Security property.
The method SecureRandom.getInstanceStrong() is called on-demand.
SecureRandom.getInstanceStrong().SecureRandom.getInstanceStrong()public boolean randomBoolean()
public byte[] randomBytes(int count)
count - the size of the returned arrayIllegalArgumentException - if count is negativepublic double randomDouble()
randomDouble(double, double)public double randomDouble(double startInclusive,
double endExclusive)
startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negativepublic float randomFloat()
randomFloat(float, float)public float randomFloat(float startInclusive,
float endExclusive)
startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negativepublic int randomInt()
randomInt(int, int)public int randomInt(int startInclusive,
int endExclusive)
startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negativepublic long randomLong()
randomLong(long, long)public long randomLong(long startInclusive,
long endExclusive)
startInclusive - the smallest value that can be returned, must be non-negativeendExclusive - the upper bound (not included)IllegalArgumentException - if startInclusive > endExclusive or if startInclusive is
negativeCopyright © 2001–2025 The Apache Software Foundation. All rights reserved.