Add sugar to RandomIUtils

This commit is contained in:
Vincent Potucek 2015-10-12 09:47:40 +02:00
parent 00fafe772c
commit 31466db6ec
2 changed files with 81 additions and 7 deletions

View File

@ -91,6 +91,15 @@ public static int nextInt(final int startInclusive, final int endExclusive) {
return startInclusive + RANDOM.nextInt(endExclusive - startInclusive); 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> * <p>
* Returns a random long within the specified range. * Returns a random long within the specified range.
@ -117,6 +126,14 @@ public static long nextLong(final long startInclusive, final long endExclusive)
return (long) nextDouble(startInclusive, endExclusive); 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> * <p>
@ -144,6 +161,15 @@ public static double nextDouble(final double startInclusive, final double endInc
return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextDouble()); 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> * <p>
* Returns a random float within the specified range. * Returns a random float within the specified range.
@ -169,4 +195,13 @@ public static float nextFloat(final float startInclusive, final float endInclusi
return startInclusive + ((endInclusive - startInclusive) * RANDOM.nextFloat()); 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);
}
} }

View File

@ -112,6 +112,16 @@ public void testNextInt() {
assertTrue(result >= 33 && result < 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. * Test next double range with minimal range.
*/ */
@ -137,6 +147,16 @@ public void testNextDouble() {
assertTrue(result >= 33d && result <= 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. * Tests next float range.
*/ */
@ -146,6 +166,16 @@ public void testNextFloat() {
assertTrue(result >= 33f && result <= 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. * Test next long range with minimal range.
*/ */
@ -163,6 +193,15 @@ public void testNextLong() {
assertTrue(result >= 33L && result < 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. * Tests extreme range.