public class YourClass { /* Oher stuff here */ private Random random; public YourClass() { // ... random = new Random(); } public boolean getRandomBoolean() { return random.nextBoolean(); } /* More stuff here */ }
public static int[] getRandomSizes(Random r) { int length = r.nextInt(10); int[] sizes = new int[length]; for (int i = 0; i < length; i++) { boolean sparse = r.nextBoolean(); if (sparse) { sizes[i] = -1; } else { sizes[i] = r.nextInt(100); } } return sizes; }
public void foo() { System.out.println("Hello"); boolean bool = Random.nextBoolean(); if (bool) return; if (bool || Random.nextBoolean()) System.out.println("World!"); }
@Test public void migration_must_create_as_much_as_rules_profile() throws SQLException { Random random = new Random(); int nbRulesProfile = 100 + random.nextInt(100); IntStream.range(0, nbRulesProfile).forEach( i -> insertRulesProfile("ORG_" + i, "java", "uuid" + i, random.nextBoolean() ? "ORG_" + random.nextInt(i + 1) : null, random.nextBoolean(), null, null)); underTest.execute(); assertThat(countRows()).isEqualTo(nbRulesProfile); }
@Test public void getBoolean_supports_heading_and_or_trailing_whitespaces() { boolean value = new Random().nextBoolean(); verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getBoolean); }
@Test public void testReverseHeaders1() { List<Block> randomChain = TestUtils.getRandomChain(new byte[32], 0, 699); List<BlockHeaderWrapper> result = new ArrayList<>(); int peerIdx = 1; Random rnd = new Random(); int cnt = 0; while (cnt < 1000) { System.out.println("Cnt: " + cnt++); Collection<SyncQueueIfc.HeadersRequest> headersRequests = syncQueue.requestHeaders(20, 5, Integer.MAX_VALUE); if (headersRequests == null) break; for (SyncQueueIfc.HeadersRequest request : headersRequests) { System.out.println("Req: " + request); List<BlockHeader> headers = rnd.nextBoolean() ? peers[peerIdx].getHeaders(request) : peers[peerIdx].getRandomHeaders(10); List<BlockHeaderWrapper> ret = syncQueue.addHeaders(createHeadersFromHeaders(headers, peer0)); result.addAll(ret); System.out.println("Result length: " + result.size());
public Font getFont(int index) { Random r = new Random(); String family = families.get(r.nextInt(families.size())); boolean bold = r.nextBoolean() && randomStyle; int size = minSize; if (maxSize - minSize > 0) { size += r.nextInt(maxSize - minSize); } return new Font(family, bold ? Font.BOLD : Font.PLAIN, size); }
Random r = new Random(); Map<Boolean, List<String>> groups = stream .collect(Collectors.partitioningBy(x -> r.nextBoolean())); System.out.println(groups.get(false).size()); System.out.println(groups.get(true).size());
@Test public void get_returns_hostname_from_NetworkUtils_if_property_is_empty_at_constructor_time() { getReturnsHostnameFromNetworkUtils(random.nextBoolean() ? "" : " "); }
public static <T> T newInstanceWithAllFieldsSet(Class<T> clz, List<RandomSupplier<?>> suppliers) { T instance = newInstance(clz); for(Field field: clz.getDeclaredFields()) { if (Modifier.isStatic(field.getModifiers())) { continue; } final Class<?> type = field.getType(); field.setAccessible(true); RandomSupplier<?> supplier = findSupplier(suppliers, type); if (supplier != null) { setField(instance, field, supplier.getNext()); } else if (type.isAssignableFrom(int.class) || type.isAssignableFrom(Integer.class)) { setField(instance, field, Math.abs(RANDOM.nextInt())); } else if (type.isAssignableFrom(long.class) || type.isAssignableFrom(Long.class)) { setField(instance, field, Math.abs(RANDOM.nextLong())); } else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) { Object bool = getField(instance, field); if (bool == null) { setField(instance, field, RANDOM.nextBoolean()); } else { setField(instance, field, !Boolean.valueOf(bool.toString())); } } else if (type.isAssignableFrom(String.class)) { setField(instance, field, UUID.randomUUID().toString()); } else { throw new RuntimeException(String.format("Could not set value for type %s no supplier available.", type)); } } return instance; }
private List<SqlDecimal> createDecimalValuesWithNull() { Random random = new Random(); List<SqlDecimal> values = new ArrayList<>(); for (int i = 0; i < ROWS; ++i) { if (random.nextBoolean()) { values.add(new SqlDecimal(BigInteger.valueOf(random.nextLong() % 10000000000L), 10, 5)); } else { values.add(null); } } return values; } }
private static Object generateRandomUnion(Random rnd) { if (rnd.nextBoolean()) { if (rnd.nextBoolean()) { return null; } else { return rnd.nextBoolean(); } } else { if (rnd.nextBoolean()) { return rnd.nextLong(); } else { return rnd.nextDouble(); } } } }
public static PersistConfig createRandom() { Random random = new Random(); String path = "/" + CommonUtils.randomAlphaNumString(random.nextInt(10)); String ufsPath = "/" + CommonUtils.randomAlphaNumString(random.nextInt(10)); long mountId = random.nextLong(); return new PersistConfig(path, mountId, random.nextBoolean(), ufsPath); }
@Test public void constructor_throws_NPE_if_type_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("type can't be null"); new Branch(new Random().nextBoolean(), "s", null); }
Random r = new Random(seed); int evenOdd = r.nextBoolean() ? 1 : 0; int source = r.nextInt(numVertices) + 1; if (source % 2 != evenOdd) { source--; int target = r.nextInt(numVertices) + 1; if (target % 2 != evenOdd) { target--;
@ExpectWarning("Bx") public static void hamlet() { Random rnd = new Random(); boolean toBe = rnd.nextBoolean(); Number result = (toBe || !toBe) ? new Integer(3) : new Float(1); System.out.println(result); }
@Test public void get_returns_host_from_NetworkUtils_getHostname_if_property_is_empty_at_constructor_time() { getReturnsHostFromNetworkUtils(random.nextBoolean() ? "" : " "); }
/** * Generates a number in [0, 2^numBits) with an exponential distribution. The floor of the log2 of * the absolute value of the result is chosen uniformly at random in [0, numBits), and then the * result is chosen from those possibilities uniformly at random. * * <p>Zero is treated as having log2 == 0. */ static double randomDouble(int maxExponent) { double result = RANDOM_SOURCE.nextDouble(); result = Math.scalb(result, RANDOM_SOURCE.nextInt(maxExponent + 1)); return RANDOM_SOURCE.nextBoolean() ? result : -result; }
private Object randomFor(Class<?> c, Random r) { if (c == boolean.class) return r.nextBoolean(); if (c == int.class) return r.nextInt(); if (c == long.class) return r.nextLong(); if (c == byte.class) return (byte)r.nextInt(); if (c == float.class) return r.nextFloat(); if (c == double.class) return r.nextDouble(); if (c == char.class) return (char)r.nextInt(); if (c == short.class) return (short)r.nextInt(); return null; }
@Override protected Boolean[] getTestData() { Random rnd = new Random(874597969123412341L); return new Boolean[] {Boolean.valueOf(true), Boolean.valueOf(false), Boolean.valueOf(rnd.nextBoolean()), Boolean.valueOf(rnd.nextBoolean()), Boolean.valueOf(rnd.nextBoolean())}; } }