/** * <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> 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> 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); }
final double dbl = RandomUtils.nextDouble(minDbl, maxDbl); data = new DataValue(column.getType(), String.valueOf(dbl)); int maxInt = (int) column.getMaxValue(); Preconditions.checkArgument((minInt > 0) && (maxInt > 0), "min and max values need to be set in configuration for integers " + column.getName()); int intVal = RandomUtils.nextInt(minInt, maxInt); data = new DataValue(column.getType(), String.valueOf(intVal)); long maxLong = column.getMaxValue(); Preconditions.checkArgument((minLong > 0) && (maxLong > 0), "min and max values need to be set in configuration for unsigned_longs " + column.getName()); long longVal = RandomUtils.nextLong(minLong, maxLong); data = new DataValue(column.getType(), String.valueOf(longVal));
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); }
private String fixedSizeByteArrayAsHexString(int length){ byte[] bBinary = RandomUtils.nextBytes(length); return DatatypeConverter.printHexBinary(bBinary); }
/** * <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); } }
private DomainObject createNewDomainObject(DomainObjectFactory objectFactory, String indexedField) { DomainObject o = objectFactory.newInstance(); o.setKey(randomAlphanumeric(7)); o.setStringVal(indexedField); o.setIntVal(nextInt(0, Integer.MAX_VALUE)); o.setLongVal(nextLong(0, Long.MAX_VALUE)); o.setDoubleVal(nextDouble(0.0, Double.MAX_VALUE)); return o; }
private String fixedSizeByteArrayAsBase64String(int length){ byte[] bBinary = RandomUtils.nextBytes(length); return DatatypeConverter.printBase64Binary(bBinary); }
@Test(expected = IllegalArgumentException.class) public void testNextFloatLowerGreaterUpper() throws Exception { RandomUtils.nextFloat(2, 1); }
private String buildDefaultApplicationName() { return DEFAULT_APPLICATION_NAME + RandomUtils.nextInt(1, 10); } }
public void resetLeaderDue() { leaderDueMs = GlobalExecutor.LEADER_TIMEOUT_MS + RandomUtils.nextLong(0, GlobalExecutor.RANDOM_MS); }
/** * <p> * Returns a random long within the specified range. * </p> * * @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 long */ public static long nextLong(final long startInclusive, final long 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 (long) nextDouble(startInclusive, endExclusive); }
@Test(expected = IllegalArgumentException.class) public void testNextBytesNegative() throws Exception { RandomUtils.nextBytes(-1); }
/** 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; }
@Override public String proxy() { String[] proxies = new String[]{"socket://127.0.0.1:8888","http://127.0.0.1:8880"}; return proxies[RandomUtils.nextInt(0,proxies.length)]; }
@Override protected long getNextKeyToRead() { // always read a random key, assuming that the writer has finished writing all keys long key = startKey + Math.abs(RandomUtils.nextLong()) % (endKey - startKey); return key; }
@Test(expected = IllegalArgumentException.class) public void testNextDoubleLowerGreaterUpper() throws Exception { RandomUtils.nextDouble(2, 1); }
@Test public void shortByteArrayIsValid() { validator.validate( RandomUtils.nextBytes( 3 ) ); validator.validate( RandomUtils.nextBytes( 30 ) ); validator.validate( RandomUtils.nextBytes( 300 ) ); validator.validate( RandomUtils.nextBytes( 4303 ) ); validator.validate( RandomUtils.nextBytes( 13234 ) ); validator.validate( RandomUtils.nextBytes( MAX_BYTE_LENGTH ) ); }
@Test(expected = IllegalArgumentException.class) public void testNextFloatNegative() throws Exception { RandomUtils.nextFloat(-1, 1); }