java.util.Random.nextBoolean()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(129)

本文整理了Java中java.util.Random.nextBoolean()方法的一些代码示例,展示了Random.nextBoolean()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random.nextBoolean()方法的具体详情如下:
包路径:java.util.Random
类名称:Random
方法名:nextBoolean

Random.nextBoolean介绍

[英]Returns a pseudo-random uniformly distributed boolean.
[中]返回伪随机均匀分布布尔值。

代码示例

代码示例来源:origin: stackoverflow.com

public class YourClass {

  /* Oher stuff here */

  private Random random;

  public YourClass() {
    // ...
    random = new Random();
  }

  public boolean getRandomBoolean() {
    return random.nextBoolean();
  }

  /* More stuff here */

}

代码示例来源:origin: stanfordnlp/CoreNLP

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;
}

代码示例来源:origin: stackoverflow.com

public void foo() {
  System.out.println("Hello");
  boolean bool = Random.nextBoolean();
  if (bool)
    return;
  if (bool || Random.nextBoolean())
   System.out.println("World!");
}

代码示例来源:origin: SonarSource/sonarqube

@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);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void getBoolean_supports_heading_and_or_trailing_whitespaces() {
 boolean value = new Random().nextBoolean();
 verifySupportHeadAndOrTrailingWhitespaces(value, Configuration::getBoolean);
}

代码示例来源:origin: ethereum/ethereumj

@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());

代码示例来源:origin: Dreampie/Resty

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);
}

代码示例来源:origin: stackoverflow.com

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());

代码示例来源:origin: SonarSource/sonarqube

@Test
public void get_returns_hostname_from_NetworkUtils_if_property_is_empty_at_constructor_time() {
 getReturnsHostnameFromNetworkUtils(random.nextBoolean() ? "" : "   ");
}

代码示例来源:origin: aws/aws-sdk-java

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;
}

代码示例来源:origin: prestodb/presto

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;
  }
}

代码示例来源:origin: apache/flink

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();
      }
    }
  }
}

代码示例来源:origin: Alluxio/alluxio

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);
}

代码示例来源:origin: SonarSource/sonarqube

@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);
}

代码示例来源:origin: apache/flink

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--;

代码示例来源:origin: spotbugs/spotbugs

@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);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void get_returns_host_from_NetworkUtils_getHostname_if_property_is_empty_at_constructor_time() {
 getReturnsHostFromNetworkUtils(random.nextBoolean() ? "" : "   ");
}

代码示例来源:origin: google/guava

/**
 * 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;
}

代码示例来源:origin: apache/avro

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;
}

代码示例来源:origin: apache/flink

@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())};
  }
}

相关文章