Merge branch 'LANG-1174'
LANG-1174: Add sugar to RandomUtils. This closes 111 from github. Thanks to github user Punkratz312.
This commit is contained in:
commit
c9cae602ec
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.5" date="tba" description="tba">
|
||||
<action issue="LANG-1174" type="add" dev="britter" dute-to="Punkratz312">Add sugar to RandomUtils</action>
|
||||
<action issue="LANG-1057" type="update" dev="chas" dute-to="Otávio Santana">Replace StringBuilder with String concatenation for better optimization</action>
|
||||
<action issue="LANG-1075" type="update" dev="chas">Deprecate SystemUtils.FILE_SEPARATOR and SystemUtils.PATH_SEPARATOR</action>
|
||||
<action issue="LANG-1154" type="add" dev="chas" due-to="Gary Gregory">FastDateFormat APIs that use a StringBuilder</action>
|
||||
|
|
|
@ -90,6 +90,17 @@ public class RandomUtils {
|
|||
|
||||
return startInclusive + RANDOM.nextInt(endExclusive - startInclusive);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> Returns a random int within 0 - Integer.MAX_VALUE </p>
|
||||
*
|
||||
* @return the random integer
|
||||
* @see #nextInt(int, int)
|
||||
* @since 3.5
|
||||
*/
|
||||
public static int nextInt() {
|
||||
return nextInt(0, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -115,8 +126,18 @@ public class RandomUtils {
|
|||
}
|
||||
|
||||
return (long) nextDouble(startInclusive, endExclusive);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> Returns a random long within 0 - Long.MAX_VALUE </p>
|
||||
*
|
||||
* @return the random long
|
||||
* @see #nextLong(long, long)
|
||||
* @since 3.5
|
||||
*/
|
||||
public static long nextLong() {
|
||||
return nextLong(0, Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -136,14 +157,25 @@ public class RandomUtils {
|
|||
Validate.isTrue(endInclusive >= startInclusive,
|
||||
"Start value must be smaller or equal to end value.");
|
||||
Validate.isTrue(startInclusive >= 0, "Both range values must be non-negative.");
|
||||
|
||||
|
||||
if (startInclusive == endInclusive) {
|
||||
return startInclusive;
|
||||
}
|
||||
|
||||
return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextDouble());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p> Returns a random double within 0 - Double.MAX_VALUE </p>
|
||||
*
|
||||
* @return the random double
|
||||
* @see #nextDouble(double, double)
|
||||
* @since 3.5
|
||||
*/
|
||||
public static double nextDouble() {
|
||||
return nextDouble(0, Double.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Returns a random float within the specified range.
|
||||
|
@ -168,5 +200,16 @@ public class RandomUtils {
|
|||
}
|
||||
|
||||
return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextFloat());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> Returns a random float within 0 - Float.MAX_VALUE </p>
|
||||
*
|
||||
* @return the random float
|
||||
* @see #nextFloat()
|
||||
* @since 3.5
|
||||
*/
|
||||
public static float nextFloat() {
|
||||
return nextFloat(0, Float.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -111,6 +111,16 @@ public class RandomUtilsTest {
|
|||
final int result = RandomUtils.nextInt(33, 42);
|
||||
assertTrue(result >= 33 && result < 42);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests next double range, random result.
|
||||
*/
|
||||
@Test
|
||||
public void testNextIntRandomResult() {
|
||||
int randomResult = RandomUtils.nextInt();
|
||||
assertTrue(randomResult > 0);
|
||||
assertTrue(randomResult < Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test next double range with minimal range.
|
||||
|
@ -136,6 +146,16 @@ public class RandomUtilsTest {
|
|||
final double result = RandomUtils.nextDouble(33d, 42d);
|
||||
assertTrue(result >= 33d && result <= 42d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests next double range, random result.
|
||||
*/
|
||||
@Test
|
||||
public void testNextDoubleRandomResult() {
|
||||
double randomResult = RandomUtils.nextDouble();
|
||||
assertTrue(randomResult > 0);
|
||||
assertTrue(randomResult < Double.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests next float range.
|
||||
|
@ -144,7 +164,17 @@ public class RandomUtilsTest {
|
|||
public void testNextFloat() {
|
||||
final double result = RandomUtils.nextFloat(33f, 42f);
|
||||
assertTrue(result >= 33f && result <= 42f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests next float range, random result.
|
||||
*/
|
||||
@Test
|
||||
public void testNextFloatRandomResult() {
|
||||
float randomResult = RandomUtils.nextFloat();
|
||||
assertTrue(randomResult > 0);
|
||||
assertTrue(randomResult < Float.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test next long range with minimal range.
|
||||
|
@ -162,7 +192,16 @@ public class RandomUtilsTest {
|
|||
final long result = RandomUtils.nextLong(33L, 42L);
|
||||
assertTrue(result >= 33L && result < 42L);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests next long range, random result.
|
||||
*/
|
||||
@Test
|
||||
public void testNextLongRandomResult() {
|
||||
long randomResult = RandomUtils.nextLong();
|
||||
assertTrue(randomResult > 0);
|
||||
assertTrue(randomResult < Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests extreme range.
|
||||
|
|
Loading…
Reference in New Issue