RandomUtils¶
- public class org.apache.commons.lang3.RandomUtils[source]¶
Supplements the standard
Randomclass.Use
secure()to get the singleton instance based onSecureRandom()which uses a secure random number generator implementing the default random number algorithm.Use
secureStrong()to get the singleton instance based onSecureRandom.getInstanceStrong()which uses an instance that was selected by using the algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.Use
insecure()to get the singleton instance based onThreadLocalRandom.current()which is not cryptographically secure. In addition, instances do not use a cryptographically random seed unless the system propertyjava.util.secureRandomSeedis set totrue.Starting in version 3.17.0, the method
secure()usesSecureRandom()instead ofSecureRandom.getInstanceStrong(), and addssecureStrong().Starting in version 3.16.0, this class uses
secure()for static methods and addsinsecure().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).
Constructors¶
Deprecated. |
- @Deprecated public RandomUtils()[source]¶
Deprecated: TODO Make private in 4.0.
RandomUtilsinstances should NOT be constructed in standard programming. Instead, the class should be used asRandomUtils.nextBytes(5);.This constructor is public to permit tools that require a JavaBean instance to operate.
Methods¶
|
Gets the singleton instance based on |
|
|
Deprecated. Generates a random boolean value. |
|
|
Deprecated. Generates an array of random bytes. |
|
|
Deprecated. Generates a random double between 0 (inclusive) and |
|
|
Deprecated. Generates a random double within the specified range. |
|
|
Deprecated. Generates a random float between 0 (inclusive) and |
|
|
Deprecated. Generates a random float within the specified range. |
|
|
Deprecated. Generates a random int between 0 (inclusive) and |
|
|
Deprecated. Generates a random integer within the specified range. |
|
|
Deprecated. Generates a random long between 0 (inclusive) and |
|
|
Deprecated. Generates a random long within the specified range. |
|
|
Generates a random boolean value. |
|
|
Generates an array of random bytes. |
|
|
Generates a random double between 0 (inclusive) and |
|
|
Generates a random double within the specified range. |
|
|
Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive). |
|
|
Generates a random float within the specified range. |
|
|
Generates a random int between 0 (inclusive) and |
|
|
Generates a random integer within the specified range. |
|
|
Generates a random long between 0 (inclusive) and |
|
|
Generates a random long within the specified range. |
|
|
Gets the singleton instance based on |
|
|
Gets the singleton instance based on |
|
- public static RandomUtils insecure()[source]¶
Gets the singleton instance based on
ThreadLocalRandom.current(); which is not cryptographically secure; for more secure processing usesecure()orsecureStrong().The method
ThreadLocalRandom.current()is called on-demand.- Returns:
The singleton instance based on
ThreadLocalRandom.current().- Since:
3.17.0
- See also:
- @Deprecated public static boolean nextBoolean()[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random boolean value.
- Returns:
The random boolean.
- Since:
3.5
- @Deprecated public static byte[] nextBytes(int count)[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates an array of random bytes.
- Parameters:
count – The size of the returned array.
- Returns:
The random byte array.
- Throws:
IllegalArgumentException– Thrown ifcountis negative.
- @Deprecated public static double nextDouble()[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random double between 0 (inclusive) and
Double.MAX_VALUE(1.7976931348623157E308 exclusive).- Returns:
The random double.
- Since:
3.5
- See also:
- @Deprecated public static double nextDouble(double startInclusive, double endExclusive)[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random double within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random double.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.
- @Deprecated public static float nextFloat()[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random float between 0 (inclusive) and
Float.MAX_VALUE(3.4028235E38f exclusive).- Returns:
The random float.
- Since:
3.5
- See also:
- @Deprecated public static float nextFloat(float startInclusive, float endExclusive)[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random float within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random float.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.
- @Deprecated public static int nextInt()[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random int between 0 (inclusive) and
Integer.MAX_VALUE(2147483647 exclusive).- Returns:
The random integer.
- Since:
3.5
- See also:
- @Deprecated public static int nextInt(int startInclusive, int endExclusive)[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random integer within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random integer.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.
- @Deprecated public static long nextLong()[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random long between 0 (inclusive) and
Long.MAX_VALUE(9223372036854775807L exclusive).- Returns:
The random long.
- Since:
3.5
- See also:
- @Deprecated public static long nextLong(long startInclusive, long endExclusive)[source]¶
Deprecated: Use
secure(),secureStrong(), orinsecure().Generates a random long within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random long.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.
- public boolean randomBoolean()[source]¶
Generates a random boolean value.
- Returns:
The random boolean.
- Since:
3.16.0
- public byte[] randomBytes(int count)[source]¶
Generates an array of random bytes.
- Parameters:
count – The size of the returned array.
- Returns:
The random byte array.
- Throws:
IllegalArgumentException– Thrown ifcountis negative.- Since:
3.16.0
- public double randomDouble()[source]¶
Generates a random double between 0 (inclusive) and
Double.MAX_VALUE(1.7976931348623157E308 exclusive).- Returns:
The random double.
- Since:
3.16.0
- See also:
- public double randomDouble(double startInclusive, double endExclusive)[source]¶
Generates a random double within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random double.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.- Since:
3.16.0
- public float randomFloat()[source]¶
Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).
- Returns:
The random float.
- Since:
3.16.0
- See also:
- public float randomFloat(float startInclusive, float endExclusive)[source]¶
Generates a random float within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random float.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.- Since:
3.16.0
- public int randomInt()[source]¶
Generates a random int between 0 (inclusive) and
Integer.MAX_VALUE(2147483647 exclusive).- Returns:
The random integer.
- Since:
3.16.0
- See also:
- public int randomInt(int startInclusive, int endExclusive)[source]¶
Generates a random integer within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random integer.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.- Since:
3.16.0
- public long randomLong()[source]¶
Generates a random long between 0 (inclusive) and
Long.MAX_VALUE(9223372036854775807L, exclusive).- Returns:
The random long.
- Since:
3.16.0
- See also:
- public long randomLong(long startInclusive, long endExclusive)[source]¶
Generates a random long within the specified range.
- Parameters:
startInclusive – The smallest value that can be returned, must be non-negative.
endExclusive – The upper bound (not included).
- Returns:
The random long.
- Throws:
IllegalArgumentException– Thrown ifstartInclusive > endExclusiveor ifstartInclusiveis negative.- Since:
3.16.0
- public static RandomUtils secure()[source]¶
Gets the singleton instance based on
SecureRandom()which uses the default algorithm and provider ofSecureRandom.The method
SecureRandom()is called on-demand.- Returns:
The singleton instance based on
SecureRandom().- Since:
3.16.0
- See also:
- public static RandomUtils secureStrong()[source]¶
Gets the singleton instance based on
SecureRandom.getInstanceStrong()which uses an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.The method
SecureRandom.getInstanceStrong()is called on-demand.- Returns:
The singleton instance based on
SecureRandom.getInstanceStrong().- Since:
3.17.0
- See also: