public class RandomUtils extends Object
Utility library that supplements the standard Random class.
Caveat: Instances of Random are not cryptographically secure.
Provides randomness between any two numbers, For example: arbitrarily large numbers, negative numbers
that may be a better choice for applications with more stringent requirements
| 构造器和说明 |
|---|
RandomUtils()
RandomUtils instances should NOT be constructed in standard
programming. |
| 限定符和类型 | 方法和说明 |
|---|---|
static BigInteger |
nextBigInteger(Number startInclusive,
Number endInclusive)
Returns a random BigInteger within the specified range.
|
static boolean |
nextBoolean()
Returns a random boolean value
|
static void |
nextBytes(byte[] b)
fill an array of random bytes.
|
static byte[] |
nextBytes(int count)
Creates an array of random bytes.
|
static BigDecimal |
nextDecimal(Integer precision,
Integer scale)
Returns a random BigDecimal within the specified range.
|
static BigDecimal |
nextDecimal(Number startInclusive,
Number endInclusive,
Integer scale)
Returns a random BigDecimal within the specified range.
|
static double |
nextDouble()
Returns a random double within 0 - Double.MAX_VALUE
|
static double |
nextDouble(double startInclusive,
double endInclusive)
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 endInclusive)
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 endInclusive)
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 endInclusive)
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 void nextBytes(byte[] b)
fill an array of random bytes.
b - fill arrayIllegalArgumentException - if count is negativepublic static int nextInt(int startInclusive,
int endInclusive)
Returns a random integer within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive 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 endInclusive)
Returns a random long within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive or if startInclusive is negativepublic static long nextLong()
Returns a random long within 0 - Long.MAX_VALUE
nextLong(long, long)public static BigInteger nextBigInteger(Number startInclusive, Number endInclusive)
Returns a random BigInteger within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive or if startInclusive is negativepublic static double nextDouble(double startInclusive,
double endInclusive)
Returns a random double within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive 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 endInclusive)
Returns a random float within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive or if startInclusive is negativepublic static float nextFloat()
Returns a random float within 0 - Float.MAX_VALUE
nextFloat()public static BigDecimal nextDecimal(Number startInclusive, Number endInclusive, Integer scale)
Returns a random BigDecimal within the specified range.
startInclusive - the smallest value that can be returned, must be non-negativeendInclusive - the upper bound (included)IllegalArgumentException - if startInclusive > endInclusive or if startInclusive is negativepublic static BigDecimal nextDecimal(Integer precision, Integer scale)
Returns a random BigDecimal within the specified range.
precision - precision of random numberscale - scale of random numberCopyright © 2022–2023. All rights reserved.