/** * <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); } }
/** Selects and returns ceil(ratio * items.length) random items from the given array */ public static <T> List<T> selectRandomItems(T[] items, float ratio) { int remaining = (int)Math.ceil(items.length * ratio); List<T> selectedItems = new ArrayList<>(remaining); for (int i=0; i<items.length && remaining > 0; i++) { if (RandomUtils.nextFloat() < ((float)remaining/(items.length-i))) { selectedItems.add(items[i]); remaining--; } } return selectedItems; }
@Test(expected = IllegalArgumentException.class) public void testNextFloatLowerGreaterUpper() throws Exception { RandomUtils.nextFloat(2, 1); }
@Test(expected = IllegalArgumentException.class) public void testNextFloatNegative() throws Exception { RandomUtils.nextFloat(-1, 1); }
/** * Tests extreme range. */ @Test public void testExtremeRangeFloat() { final float result = RandomUtils.nextFloat(0, Float.MAX_VALUE); assertTrue(result >= 0f && result <= Float.MAX_VALUE); }
/** * Test next float range with minimal range. */ @Test public void testNextFloatMinimalRange() { assertEquals(42.1f, RandomUtils.nextFloat(42.1f, 42.1f), DELTA); }
/** * Tests next float range. */ @Test 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() { final float randomResult = RandomUtils.nextFloat(); assertTrue(randomResult > 0); assertTrue(randomResult < Float.MAX_VALUE); }
/** * <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); } }
/** * <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); } }
/** * <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); } }
@Override public Object handle(Task aTask) throws Exception { float nextFloat = RandomUtils.nextFloat(0, 1); float probabilty = aTask.getFloat("probabilty",0.5f); Assert.isTrue(probabilty>=0 && probabilty<=1,"probability must be a value between 0 and 1"); if(nextFloat <= probabilty) { throw new IllegalStateException("I'm a rogue exception"); } return null; }
public static FloatMatrix rand(int row, int col, float max) { float[][] result = new float[row][col]; for (int i=0; i<row; i++) { for (int j=0; j<col; j++) { if (RandomUtils.nextInt(0, 2) == 0) { result[i][j] = RandomUtils.nextFloat(0, max); } else { result[i][j] = 0-RandomUtils.nextFloat(0, max); } } } return new FloatMatrix(result); }
/** Selects and returns ceil(ratio * items.length) random items from the given array */ public static <T> List<T> selectRandomItems(T[] items, float ratio) { int remaining = (int)Math.ceil(items.length * ratio); List<T> selectedItems = new ArrayList<>(remaining); for (int i=0; i<items.length && remaining > 0; i++) { if (RandomUtils.nextFloat() < ((float)remaining/(items.length-i))) { selectedItems.add(items[i]); remaining--; } } return selectedItems; }