Repository: commons-lang Updated Branches: refs/heads/master 881c5fa3d -> c9cae602e
Add sugar to RandomIUtils Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/31466db6 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/31466db6 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/31466db6 Branch: refs/heads/master Commit: 31466db6ecd115aa74616e14a75721b45c0f59f9 Parents: 00fafe7 Author: Vincent Potucek <[email protected]> Authored: Mon Oct 12 09:47:40 2015 +0200 Committer: Vincent Potucek <[email protected]> Committed: Mon Oct 12 09:47:40 2015 +0200 ---------------------------------------------------------------------- .../org/apache/commons/lang3/RandomUtils.java | 45 +++++++++++++++++--- .../apache/commons/lang3/RandomUtilsTest.java | 43 ++++++++++++++++++- 2 files changed, 81 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/31466db6/src/main/java/org/apache/commons/lang3/RandomUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/RandomUtils.java b/src/main/java/org/apache/commons/lang3/RandomUtils.java index 6bba274..16188c6 100644 --- a/src/main/java/org/apache/commons/lang3/RandomUtils.java +++ b/src/main/java/org/apache/commons/lang3/RandomUtils.java @@ -90,6 +90,15 @@ public class RandomUtils { return startInclusive + RANDOM.nextInt(endExclusive - startInclusive); } + + /** + * <p> Returns a random int within 0 - Integer.MAX_VALUE </p> + * + * @see #nextInt() + */ + public static int nextInt() { + return nextInt(0, Integer.MAX_VALUE); + } /** * <p> @@ -115,8 +124,16 @@ public class RandomUtils { } return (long) nextDouble(startInclusive, endExclusive); - } - + } + + /** + * <p> Returns a random long within 0 - Long.MAX_VALUE </p> + * + * @see #nextLong() + */ + public static long nextLong() { + return nextLong(0, Long.MAX_VALUE); + } /** * <p> @@ -136,14 +153,23 @@ 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> + * + * @see #nextDouble() + */ + public static double nextDouble() { + return nextDouble(0, Double.MAX_VALUE); + } + /** * <p> * Returns a random float within the specified range. @@ -168,5 +194,14 @@ public class RandomUtils { } return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextFloat()); - } + } + + /** + * <p> Returns a random float within 0 - Float.MAX_VALUE </p> + * + * @see #nextFloat() + */ + public static float nextFloat() { + return nextFloat(0, Float.MAX_VALUE); + } } http://git-wip-us.apache.org/repos/asf/commons-lang/blob/31466db6/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java index a2aa063..e1108e5 100644 --- a/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomUtilsTest.java @@ -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.
