From 2978cb6cf05379fd42374eb5a235be84e021f247 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 18 Apr 2021 19:21:44 -0400 Subject: [PATCH] DMI: Random object created and used only once (DMI_RANDOM_USED_ONLY_ONCE); Better multi-threaded behavior. Thank you SpotBugs. Sort methods. --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ArrayUtils.java | 23 +- .../commons/lang3/RandomStringUtils.java | 412 +++++++++--------- .../org/apache/commons/lang3/RandomUtils.java | 245 ++++++----- 4 files changed, 342 insertions(+), 339 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 472eff51e..f508683a0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -49,6 +49,7 @@ The type attribute can be add,update,fix,remove. NumberUtils.createNumber() to recognize hex integers prefixed with +. NumberUtils.createNumber() to return requested floating point type for zero. + DMI: Random object created and used only once (DMI_RANDOM_USED_ONLY_ONCE); Better multi-threaded behavior. Add EnumUtils.getEnumSystemProperty(...). Add TriConsumer. diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 33acf3e74..9f115fe53 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -26,6 +26,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -4644,6 +4645,10 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd return array; } + private static ThreadLocalRandom random() { + return ThreadLocalRandom.current(); + } + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (subtracts one from @@ -7659,7 +7664,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final boolean[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7684,7 +7689,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final byte[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7709,7 +7714,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final char[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7734,7 +7739,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final double[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7759,7 +7764,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final float[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7784,7 +7789,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final int[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7809,7 +7814,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final long[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7834,7 +7839,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final Object[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** @@ -7859,7 +7864,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd * @since 3.6 */ public static void shuffle(final short[] array) { - shuffle(array, new Random()); + shuffle(array, random()); } /** diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java index 6f0242a31..835c8a306 100644 --- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java +++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /** *

Generates random {@code String}s.

@@ -48,22 +49,8 @@ import java.util.Random; */ public class RandomStringUtils { - /** - *

Random object used by random method. This has to be not local - * to the random method so as to not return the same value in the - * same millisecond.

- */ - private static final Random RANDOM = new Random(); - - /** - *

{@code RandomStringUtils} instances should NOT be constructed in - * standard programming. Instead, the class should be used as - * {@code RandomStringUtils.random(5);}.

- * - *

This constructor is public to permit tools that require a JavaBean instance - * to operate.

- */ - public RandomStringUtils() { + private static ThreadLocalRandom random() { + return ThreadLocalRandom.current(); } // Random @@ -81,184 +68,6 @@ public class RandomStringUtils { return random(count, false, false); } - /** - *

Creates a random string whose length is the number of characters - * specified.

- * - *

Characters will be chosen from the set of characters whose - * ASCII value is between {@code 32} and {@code 126} (inclusive).

- * - * @param count the length of random string to create - * @return the random string - */ - public static String randomAscii(final int count) { - return random(count, 32, 127, false, false); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of characters whose - * ASCII value is between {@code 32} and {@code 126} (inclusive).

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive) { - return randomAscii(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - - /** - *

Creates a random string whose length is the number of characters - * specified.

- * - *

Characters will be chosen from the set of Latin alphabetic - * characters (a-z, A-Z).

- * - * @param count the length of random string to create - * @return the random string - */ - public static String randomAlphabetic(final int count) { - return random(count, true, false); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive) { - return randomAlphabetic(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - - /** - *

Creates a random string whose length is the number of characters - * specified.

- * - *

Characters will be chosen from the set of Latin alphabetic - * characters (a-z, A-Z) and the digits 0-9.

- * - * @param count the length of random string to create - * @return the random string - */ - public static String randomAlphanumeric(final int count) { - return random(count, true, true); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of Latin alphabetic - * characters (a-z, A-Z) and the digits 0-9.

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive) { - return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - - /** - *

Creates a random string whose length is the number of characters specified.

- * - *

Characters will be chosen from the set of characters which match the POSIX [:graph:] - * regular expression character class. This class contains all visible ASCII characters - * (i.e. anything except spaces and control characters).

- * - * @param count the length of random string to create - * @return the random string - * @since 3.5 - */ - public static String randomGraph(final int count) { - return random(count, 33, 126, false, false); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of \p{Graph} characters.

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { - return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - - /** - *

Creates a random string whose length is the number of characters - * specified.

- * - *

Characters will be chosen from the set of numeric - * characters.

- * - * @param count the length of random string to create - * @return the random string - */ - public static String randomNumeric(final int count) { - return random(count, false, true); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of \p{Digit} characters.

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomNumeric(final int minLengthInclusive, final int maxLengthExclusive) { - return randomNumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - - /** - *

Creates a random string whose length is the number of characters specified.

- * - *

Characters will be chosen from the set of characters which match the POSIX [:print:] - * regular expression character class. This class includes all visible ASCII characters and spaces - * (i.e. anything except control characters).

- * - * @param count the length of random string to create - * @return the random string - * @since 3.5 - */ - public static String randomPrint(final int count) { - return random(count, 32, 126, false, false); - } - - /** - *

Creates a random string whose length is between the inclusive minimum and - * the exclusive maximum.

- * - *

Characters will be chosen from the set of \p{Print} characters.

- * - * @param minLengthInclusive the inclusive minimum length of the string to generate - * @param maxLengthExclusive the exclusive maximum length of the string to generate - * @return the random string - * @since 3.5 - */ - public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { - return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); - } - /** *

Creates a random string whose length is the number of characters * specified.

@@ -277,6 +86,25 @@ public class RandomStringUtils { return random(count, 0, 0, letters, numbers); } + /** + *

Creates a random string whose length is the number of characters + * specified.

+ * + *

Characters will be chosen from the set of characters specified.

+ * + * @param count the length of random string to create + * @param chars the character array containing the set of characters to use, + * may be null + * @return the random string + * @throws IllegalArgumentException if {@code count} < 0. + */ + public static String random(final int count, final char... chars) { + if (chars == null) { + return random(count, 0, 0, false, false, null, random()); + } + return random(count, 0, chars.length, false, false, chars, random()); + } + /** *

Creates a random string whose length is the number of characters * specified.

@@ -294,7 +122,7 @@ public class RandomStringUtils { * @return the random string */ public static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers) { - return random(count, start, end, letters, numbers, null, RANDOM); + return random(count, start, end, letters, numbers, null, random()); } /** @@ -320,7 +148,7 @@ public class RandomStringUtils { * {@code (end - start) + 1} characters in the set array. */ public static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers, final char... chars) { - return random(count, start, end, letters, numbers, chars, RANDOM); + return random(count, start, end, letters, numbers, chars, random()); } /** @@ -434,7 +262,6 @@ public class RandomStringUtils { return builder.toString(); } - /** *

Creates a random string whose length is the number of characters * specified.

@@ -451,7 +278,7 @@ public class RandomStringUtils { */ public static String random(final int count, final String chars) { if (chars == null) { - return random(count, 0, 0, false, false, null, RANDOM); + return random(count, 0, 0, false, false, null, random()); } return random(count, chars.toCharArray()); } @@ -460,19 +287,190 @@ public class RandomStringUtils { *

Creates a random string whose length is the number of characters * specified.

* - *

Characters will be chosen from the set of characters specified.

+ *

Characters will be chosen from the set of Latin alphabetic + * characters (a-z, A-Z).

* * @param count the length of random string to create - * @param chars the character array containing the set of characters to use, - * may be null * @return the random string - * @throws IllegalArgumentException if {@code count} < 0. */ - public static String random(final int count, final char... chars) { - if (chars == null) { - return random(count, 0, 0, false, false, null, RANDOM); - } - return random(count, 0, chars.length, false, false, chars, RANDOM); + public static String randomAlphabetic(final int count) { + return random(count, true, false); + } + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive) { + return randomAlphabetic(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

Creates a random string whose length is the number of characters + * specified.

+ * + *

Characters will be chosen from the set of Latin alphabetic + * characters (a-z, A-Z) and the digits 0-9.

+ * + * @param count the length of random string to create + * @return the random string + */ + public static String randomAlphanumeric(final int count) { + return random(count, true, true); + } + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of Latin alphabetic + * characters (a-z, A-Z) and the digits 0-9.

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive) { + return randomAlphanumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

Creates a random string whose length is the number of characters + * specified.

+ * + *

Characters will be chosen from the set of characters whose + * ASCII value is between {@code 32} and {@code 126} (inclusive).

+ * + * @param count the length of random string to create + * @return the random string + */ + public static String randomAscii(final int count) { + return random(count, 32, 127, false, false); + } + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of characters whose + * ASCII value is between {@code 32} and {@code 126} (inclusive).

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomAscii(final int minLengthInclusive, final int maxLengthExclusive) { + return randomAscii(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

Creates a random string whose length is the number of characters specified.

+ * + *

Characters will be chosen from the set of characters which match the POSIX [:graph:] + * regular expression character class. This class contains all visible ASCII characters + * (i.e. anything except spaces and control characters).

+ * + * @param count the length of random string to create + * @return the random string + * @since 3.5 + */ + public static String randomGraph(final int count) { + return random(count, 33, 126, false, false); + } + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of \p{Graph} characters.

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { + return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

Creates a random string whose length is the number of characters + * specified.

+ * + *

Characters will be chosen from the set of numeric + * characters.

+ * + * @param count the length of random string to create + * @return the random string + */ + public static String randomNumeric(final int count) { + return random(count, false, true); + } + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of \p{Digit} characters.

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomNumeric(final int minLengthInclusive, final int maxLengthExclusive) { + return randomNumeric(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

Creates a random string whose length is the number of characters specified.

+ * + *

Characters will be chosen from the set of characters which match the POSIX [:print:] + * regular expression character class. This class includes all visible ASCII characters and spaces + * (i.e. anything except control characters).

+ * + * @param count the length of random string to create + * @return the random string + * @since 3.5 + */ + public static String randomPrint(final int count) { + return random(count, 32, 126, false, false); + } + + + /** + *

Creates a random string whose length is between the inclusive minimum and + * the exclusive maximum.

+ * + *

Characters will be chosen from the set of \p{Print} characters.

+ * + * @param minLengthInclusive the inclusive minimum length of the string to generate + * @param maxLengthExclusive the exclusive maximum length of the string to generate + * @return the random string + * @since 3.5 + */ + public static String randomPrint(final int minLengthInclusive, final int maxLengthExclusive) { + return randomPrint(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); + } + + /** + *

{@code RandomStringUtils} instances should NOT be constructed in + * standard programming. Instead, the class should be used as + * {@code RandomStringUtils.random(5);}.

+ * + *

This constructor is public to permit tools that require a JavaBean instance + * to operate.

+ */ + public RandomStringUtils() { } } diff --git a/src/main/java/org/apache/commons/lang3/RandomUtils.java b/src/main/java/org/apache/commons/lang3/RandomUtils.java index 6f963d1ea..38fa8dcb4 100644 --- a/src/main/java/org/apache/commons/lang3/RandomUtils.java +++ b/src/main/java/org/apache/commons/lang3/RandomUtils.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /** *

Utility library that supplements the standard {@link Random} class.

@@ -33,27 +34,6 @@ import java.util.Random; */ public class RandomUtils { - /** - * Random object used by random method. This has to be not local to the - * random method so as to not return the same value in the same millisecond. - */ - private static final Random RANDOM = new Random(); - - /** - *

- * {@code RandomUtils} instances should NOT be constructed in standard - * programming. Instead, the class should be used as - * {@code RandomUtils.nextBytes(5);}. - *

- * - *

- * This constructor is public to permit tools that require a JavaBean - * instance to operate. - *

- */ - public RandomUtils() { - } - /** *

* Returns a random boolean value @@ -63,7 +43,7 @@ public class RandomUtils { * @since 3.5 */ public static boolean nextBoolean() { - return RANDOM.nextBoolean(); + return random().nextBoolean(); } /** @@ -80,10 +60,95 @@ public class RandomUtils { Validate.isTrue(count >= 0, "Count cannot be negative."); final byte[] result = new byte[count]; - RANDOM.nextBytes(result); + random().nextBytes(result); return result; } + /** + *

Returns a random double within 0 - Double.MAX_VALUE

+ * + * @return the random double + * @see #nextDouble(double, double) + * @since 3.5 + */ + public static double nextDouble() { + return nextDouble(0, Double.MAX_VALUE); + } + + /** + *

+ * Returns a random double within the specified range. + *

+ * + * @param startInclusive + * the smallest value that can be returned, must be non-negative + * @param endExclusive + * the upper bound (not included) + * @throws IllegalArgumentException + * if {@code startInclusive > endExclusive} or if + * {@code startInclusive} is negative + * @return the random double + */ + public static double nextDouble(final double startInclusive, final double endExclusive) { + Validate.isTrue(endExclusive >= startInclusive, + "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) { + return startInclusive; + } + + return startInclusive + ((endExclusive - startInclusive) * random().nextDouble()); + } + + /** + *

Returns a random float within 0 - Float.MAX_VALUE

+ * + * @return the random float + * @see #nextFloat(float, float) + * @since 3.5 + */ + public static float nextFloat() { + return nextFloat(0, Float.MAX_VALUE); + } + + /** + *

+ * Returns a random float within the specified range. + *

+ * + * @param startInclusive + * the smallest value that can be returned, must be non-negative + * @param endExclusive + * the upper bound (not included) + * @throws IllegalArgumentException + * if {@code startInclusive > endExclusive} or if + * {@code startInclusive} is negative + * @return the random float + */ + public static float nextFloat(final float startInclusive, final float endExclusive) { + Validate.isTrue(endExclusive >= startInclusive, + "Start value must be smaller or equal to end value."); + Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); + + if (startInclusive == endExclusive) { + return startInclusive; + } + + return startInclusive + ((endExclusive - startInclusive) * random().nextFloat()); + } + + /** + *

Returns a random int within 0 - Integer.MAX_VALUE

+ * + * @return the random integer + * @see #nextInt(int, int) + * @since 3.5 + */ + public static int nextInt() { + return nextInt(0, Integer.MAX_VALUE); + } + /** *

* Returns a random integer within the specified range. @@ -107,18 +172,38 @@ public class RandomUtils { return startInclusive; } - return startInclusive + RANDOM.nextInt(endExclusive - startInclusive); + return startInclusive + random().nextInt(endExclusive - startInclusive); } /** - *

Returns a random int within 0 - Integer.MAX_VALUE

+ *

Returns a random long within 0 - Long.MAX_VALUE

* - * @return the random integer - * @see #nextInt(int, int) + * @return the random long + * @see #nextLong(long, long) * @since 3.5 */ - public static int nextInt() { - return nextInt(0, Integer.MAX_VALUE); + public static long nextLong() { + return nextLong(Long.MAX_VALUE); + } + + /** + * Generates a {@code long} value between 0 (inclusive) and the specified + * value (exclusive). + * + * @param n Bound on the random number to be returned. Must be positive. + * @return a random {@code long} value between 0 (inclusive) and {@code n} + * (exclusive). + */ + private static long nextLong(final long n) { + // Extracted from o.a.c.rng.core.BaseProvider.nextLong(long) + long bits; + long val; + do { + bits = random().nextLong() >>> 1; + val = bits % n; + } while (bits - val + (n - 1) < 0); + + return val; } /** @@ -147,108 +232,22 @@ public class RandomUtils { return startInclusive + nextLong(endExclusive - startInclusive); } - /** - *

Returns a random long within 0 - Long.MAX_VALUE

- * - * @return the random long - * @see #nextLong(long, long) - * @since 3.5 - */ - public static long nextLong() { - return nextLong(Long.MAX_VALUE); - } - - /** - * Generates a {@code long} value between 0 (inclusive) and the specified - * value (exclusive). - * - * @param n Bound on the random number to be returned. Must be positive. - * @return a random {@code long} value between 0 (inclusive) and {@code n} - * (exclusive). - */ - private static long nextLong(final long n) { - // Extracted from o.a.c.rng.core.BaseProvider.nextLong(long) - long bits; - long val; - do { - bits = RANDOM.nextLong() >>> 1; - val = bits % n; - } while (bits - val + (n - 1) < 0); - - return val; + private static ThreadLocalRandom random() { + return ThreadLocalRandom.current(); } /** *

- * Returns a random double within the specified range. + * {@code RandomUtils} instances should NOT be constructed in standard + * programming. Instead, the class should be used as + * {@code RandomUtils.nextBytes(5);}. *

* - * @param startInclusive - * the smallest value that can be returned, must be non-negative - * @param endExclusive - * the upper bound (not included) - * @throws IllegalArgumentException - * if {@code startInclusive > endExclusive} or if - * {@code startInclusive} is negative - * @return the random double - */ - public static double nextDouble(final double startInclusive, final double endExclusive) { - Validate.isTrue(endExclusive >= startInclusive, - "Start value must be smaller or equal to end value."); - Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); - - if (startInclusive == endExclusive) { - return startInclusive; - } - - return startInclusive + ((endExclusive - startInclusive) * RANDOM.nextDouble()); - } - - /** - *

Returns a random double within 0 - Double.MAX_VALUE

- * - * @return the random double - * @see #nextDouble(double, double) - * @since 3.5 - */ - public static double nextDouble() { - return nextDouble(0, Double.MAX_VALUE); - } - - /** *

- * Returns a random float within the specified range. + * This constructor is public to permit tools that require a JavaBean + * instance to operate. *

- * - * @param startInclusive - * the smallest value that can be returned, must be non-negative - * @param endExclusive - * the upper bound (not included) - * @throws IllegalArgumentException - * if {@code startInclusive > endExclusive} or if - * {@code startInclusive} is negative - * @return the random float */ - public static float nextFloat(final float startInclusive, final float endExclusive) { - Validate.isTrue(endExclusive >= startInclusive, - "Start value must be smaller or equal to end value."); - Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative."); - - if (startInclusive == endExclusive) { - return startInclusive; - } - - return startInclusive + ((endExclusive - startInclusive) * RANDOM.nextFloat()); - } - - /** - *

Returns a random float within 0 - Float.MAX_VALUE

- * - * @return the random float - * @see #nextFloat(float, float) - * @since 3.5 - */ - public static float nextFloat() { - return nextFloat(0, Float.MAX_VALUE); + public RandomUtils() { } }