Class RandomUtils
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).
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic RandomUtilsinsecure()Gets the singleton instance based onThreadLocalRandom.current(); which is not cryptographically secure; usesecure()to use an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.static booleanDeprecated.static byte[]nextBytes(int count) Deprecated.static doubleDeprecated.static doublenextDouble(double startInclusive, double endExclusive) Deprecated.static floatDeprecated.static floatnextFloat(float startInclusive, float endExclusive) Deprecated.static intnextInt()Deprecated.static intnextInt(int startInclusive, int endExclusive) Deprecated.static longnextLong()Deprecated.static longnextLong(long startInclusive, long endExclusive) Deprecated.booleanGenerates a random boolean value.byte[]randomBytes(int count) Generates an array of random bytes.doubleGenerates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).doublerandomDouble(double startInclusive, double endExclusive) Generates a random double within the specified range.floatGenerates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).floatrandomFloat(float startInclusive, float endExclusive) Generates a random float within the specified range.intGenerates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).intrandomInt(int startInclusive, int endExclusive) Generates a random integer within the specified range.longGenerates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).longrandomLong(long startInclusive, long endExclusive) Generates a random long within the specified range.static RandomUtilssecure()Gets the singleton instance based onSecureRandom()which uses an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.static RandomUtilsGets the singleton instance based onSecureRandom.getInstanceStrong()which uses an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.toString()
- 
Constructor Details- 
RandomUtilsDeprecated.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. 
 
- 
- 
Method Details- 
insecureGets the singleton instance based onThreadLocalRandom.current(); which is not cryptographically secure; usesecure()to use an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.The method ThreadLocalRandom.current()is called on-demand.- Returns:
- the singleton instance based on ThreadLocalRandom.current().
- Since:
- 3.17.0
- See Also:
 
- 
nextBooleanDeprecated.Generates a random boolean value.- Returns:
- the random boolean
- Since:
- 3.5
 
- 
nextBytesDeprecated.Generates an array of random bytes.- Parameters:
- count- the size of the returned array
- Returns:
- the random byte array
- Throws:
- IllegalArgumentException- if- countis negative
 
- 
nextDoubleDeprecated.Generates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).- Returns:
- the random double
- Since:
- 3.5
- See Also:
 
- 
nextDoubleDeprecated.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- if- startInclusive > endExclusiveor if- startInclusiveis negative
 
- 
nextFloatDeprecated.Generates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).- Returns:
- the random float
- Since:
- 3.5
- See Also:
 
- 
nextFloatDeprecated.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- if- startInclusive > endExclusiveor if- startInclusiveis negative
 
- 
nextIntDeprecated.Generates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).- Returns:
- the random integer
- Since:
- 3.5
- See Also:
 
- 
nextIntDeprecated.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- if- startInclusive > endExclusiveor if- startInclusiveis negative
 
- 
nextLongDeprecated.Generates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).- Returns:
- the random long
- Since:
- 3.5
- See Also:
 
- 
nextLongDeprecated.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- if- startInclusive > endExclusiveor if- startInclusiveis negative
 
- 
secureGets the singleton instance based onSecureRandom()which uses an algorithms/providers specified in thesecurerandom.strongAlgorithmsSecurityproperty.The method SecureRandom()is called on-demand.- Returns:
- the singleton instance based on SecureRandom().
- Since:
- 3.16.0
- See Also:
 
- 
secureStrongGets the singleton instance based onSecureRandom.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:
 
- 
randomBooleanGenerates a random boolean value.- Returns:
- the random boolean
- Since:
- 3.16.0
 
- 
randomBytesGenerates an array of random bytes.- Parameters:
- count- the size of the returned array
- Returns:
- the random byte array
- Throws:
- IllegalArgumentException- if- countis negative
- Since:
- 3.16.0
 
- 
randomDoubleGenerates a random double between 0 (inclusive) and Double.MAX_VALUE (exclusive).- Returns:
- the random double
- Since:
- 3.16.0
- See Also:
 
- 
randomDoubleGenerates 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- if- startInclusive > endExclusiveor if- startInclusiveis negative
- Since:
- 3.16.0
 
- 
randomFloatGenerates a random float between 0 (inclusive) and Float.MAX_VALUE (exclusive).- Returns:
- the random float
- Since:
- 3.16.0
- See Also:
 
- 
randomFloatGenerates 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- if- startInclusive > endExclusiveor if- startInclusiveis negative
 
- 
randomIntGenerates a random int between 0 (inclusive) and Integer.MAX_VALUE (exclusive).- Returns:
- the random integer
- Since:
- 3.16.0
- See Also:
 
- 
randomIntGenerates 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- if- startInclusive > endExclusiveor if- startInclusiveis negative
- Since:
- 3.16.0
 
- 
randomLongGenerates a random long between 0 (inclusive) and Long.MAX_VALUE (exclusive).- Returns:
- the random long
- Since:
- 3.16.0
- See Also:
 
- 
randomLongGenerates 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- if- startInclusive > endExclusiveor if- startInclusiveis negative
- Since:
- 3.16.0
 
- 
toString
 
-