public class RandomUtils extends Object
Utility library that supplements the standard Random
class.
Caveat: Instances of Random
are 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 and Description |
---|
RandomUtils()
RandomUtils instances should NOT be constructed in standard
programming. |
Modifier and Type | Method and Description |
---|---|
static boolean |
nextBoolean()
Returns a random boolean value
|
static byte[] |
nextBytes(int count)
Creates an array of random bytes.
|
static double |
nextDouble()
Returns a random double within 0 - Double.MAX_VALUE
|
static double |
nextDouble(double startInclusive,
double endExclusive)
Returns a random double within the specified range.
|
static float |
nextFloat()
Returns a random float within 0 - Float.MAX_VALUE
|
static float |
nextFloat(float startInclusive,
float endExclusive)
Returns a random float within the specified range.
|
static int |
nextInt()
Returns a random int within 0 - Integer.MAX_VALUE
|
static int |
nextInt(int startInclusive,
int endExclusive)
Returns a random integer within the specified range.
|
static long |
nextLong()
Returns a random long within 0 - Long.MAX_VALUE
|
static long |
nextLong(long startInclusive,
long endExclusive)
Returns a random long within the specified range.
|
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 boolean nextBoolean()
Returns a random boolean value
public static byte[] nextBytes(int count)
Creates an array of random bytes.
count
- the size of the returned arrayIllegalArgumentException
- if count
is negativepublic static int nextInt(int startInclusive, int endExclusive)
Returns a random integer within the specified range.
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 int nextInt()
Returns a random int within 0 - Integer.MAX_VALUE
nextInt(int, int)
public static long nextLong(long startInclusive, long endExclusive)
Returns a random long within the specified range.
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 long nextLong()
Returns a random long within 0 - Long.MAX_VALUE
nextLong(long, long)
public static double nextDouble(double startInclusive, double endExclusive)
Returns a random double within the specified range.
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 double nextDouble()
Returns a random double within 0 - Double.MAX_VALUE
nextDouble(double, double)
public static float nextFloat(float startInclusive, float endExclusive)
Returns a random float within the specified range.
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 float nextFloat()
Returns a random float within 0 - Float.MAX_VALUE
nextFloat(float, float)
Copyright © 2001–2021 The Apache Software Foundation. All rights reserved.