List of usage examples for java.util Random nextBoolean
public boolean nextBoolean()
From source file:org.wso2.carbon.sample.performance.Client.java
private static void publishEventsForLatency(DataPublisher dataPublisher, long eventCount, long elapsedCount, long warmUpCount) { sendWarmUpEvents(dataPublisher, warmUpCount); long counter = 0; Random randomGenerator = new Random(); String streamId = "org.wso2.event.sensor.stream:1.0.0"; while (counter < eventCount) { boolean isPowerSaveEnabled = randomGenerator.nextBoolean(); int sensorId = randomGenerator.nextInt(); double longitude = randomGenerator.nextDouble(); double latitude = randomGenerator.nextDouble(); float humidity = randomGenerator.nextFloat(); double sensorValue = randomGenerator.nextDouble(); Event event = new Event(streamId, System.currentTimeMillis(), new Object[] { System.currentTimeMillis(), isPowerSaveEnabled, sensorId, "temperature-" + counter }, new Object[] { longitude, latitude }, new Object[] { humidity, sensorValue }); dataPublisher.publish(event);//from w w w .ja v a 2 s . c o m log.info("Sent event " + counter + " at " + System.currentTimeMillis()); if (elapsedCount > 0) { try { Thread.sleep(elapsedCount); } catch (InterruptedException e) { e.printStackTrace(); } } counter++; } }
From source file:cop.raml.utils.example.JsonExample.java
/** * Generates random example for given primitive {@code kind} using given {@code random} generator. * * @param kind primitive kind type//from www . ja va2 s.c o m * @param random not {@code null} random generator * @return generated random example for given primitive type */ private static Object getPrimitiveExample(TypeKind kind, @NotNull Random random) { if (kind == TypeKind.BOOLEAN) return random.nextBoolean(); if (kind == TypeKind.BYTE) return (byte) random.nextInt(255); if (kind == TypeKind.SHORT) return (short) random.nextInt(1000); if (kind == TypeKind.INT) return random.nextInt(1000); if (kind == TypeKind.LONG) return (long) random.nextInt(1000); if (kind == TypeKind.CHAR) return (char) ('A' + random.nextInt(28)); if (kind == TypeKind.FLOAT) return (float) random.nextInt(1000); if (kind == TypeKind.DOUBLE) return (double) random.nextInt(1000); return null; }
From source file:org.apache.hadoop.hdfs.protocol.RandomObjectsGenerators.java
static LocatedBlock[] rndLocatedBlockArr(Random rnd, int count) { LocatedBlock[] blockLocations = new LocatedBlock[count]; for (int i = 0; i < blockLocations.length; i++) { blockLocations[i] = new LocatedBlock(rndBlock(rnd), rndDatanodeInfoArr(rnd, 2), rnd.nextInt(), rnd.nextBoolean()); }/* w ww .ja va 2s .c o m*/ return blockLocations; }
From source file:org.apache.hadoop.hdfs.protocol.RandomObjectsGenerators.java
static LocatedBlocks[] rndLocatedBlocksArr(Random rnd, int count) { LocatedBlocks[] blockLocations = new LocatedBlocks[count]; for (int i = 0; i < blockLocations.length; i++) { blockLocations[i] = new LocatedBlocks(rnd.nextInt(10000), Arrays.asList(rndLocatedBlockArr(rnd, 10)), rnd.nextBoolean()); }/*from w w w . j a v a 2 s . co m*/ return blockLocations; }
From source file:org.apache.hadoop.hbase.io.hfile.TestCacheOnWrite.java
public static KeyValue.Type generateKeyType(Random rand) { if (rand.nextBoolean()) { // Let's make half of KVs puts. return KeyValue.Type.Put; } else {/*from ww w . j a v a 2 s. c om*/ KeyValue.Type keyType = KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)]; if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) { throw new RuntimeException("Generated an invalid key type: " + keyType + ". " + "Probably the layout of KeyValue.Type has changed."); } return keyType; } }
From source file:org.wso2.carbon.sample.performance.Client.java
private static void publishEvents(DataPublisher dataPublisher, long eventCount, long elapsedCount, long warmUpCount) { long counter = 0; Random randomGenerator = new Random(); String streamId = "org.wso2.event.sensor.stream:1.0.0"; long lastTime = System.currentTimeMillis(); DecimalFormat decimalFormat = new DecimalFormat("#"); while (counter < eventCount) { boolean isPowerSaveEnabled = randomGenerator.nextBoolean(); int sensorId = randomGenerator.nextInt(); double longitude = randomGenerator.nextDouble(); double latitude = randomGenerator.nextDouble(); float humidity = randomGenerator.nextFloat(); double sensorValue = randomGenerator.nextDouble(); Event event = new Event(streamId, System.currentTimeMillis(), new Object[] { System.currentTimeMillis(), isPowerSaveEnabled, sensorId, "temperature-" + counter }, new Object[] { longitude, latitude }, new Object[] { humidity, sensorValue }); dataPublisher.publish(event);/*from w w w .j a va 2s. c o m*/ if ((counter > warmUpCount) && ((counter + 1) % elapsedCount == 0)) { long currentTime = System.currentTimeMillis(); long elapsedTime = currentTime - lastTime; double throughputPerSecond = (((double) elapsedCount) / elapsedTime) * 1000; lastTime = currentTime; log.info("Sent " + elapsedCount + " sensor events in " + elapsedTime + " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond) + " events per second."); } counter++; } }
From source file:org.elasticsearch.client.RestClientTestUtil.java
/** * Create a random number of {@link Header}s. * Generated header names will either be the {@code baseName} plus its index, or exactly the provided {@code baseName} so that the * we test also support for multiple headers with same key and different values. *//*from w w w . j av a2s . c o m*/ static Header[] randomHeaders(Random random, final String baseName) { int numHeaders = RandomNumbers.randomIntBetween(random, 0, 5); final Header[] headers = new Header[numHeaders]; for (int i = 0; i < numHeaders; i++) { String headerName = baseName; //randomly exercise the code path that supports multiple headers with same key if (random.nextBoolean()) { headerName = headerName + i; } headers[i] = new BasicHeader(headerName, RandomStrings.randomAsciiOfLengthBetween(random, 3, 10)); } return headers; }
From source file:de.hub.cs.dbis.aeolus.queries.utils.TimestampMergerTest.java
@BeforeClass public static void prepareStatic() { long seed = System.currentTimeMillis(); Random r = new Random(seed); System.out.println("Static test seed: " + seed); tsIndex = r.nextInt();//www. j a v a 2 s . c om if (tsIndex < 0) { tsIndex *= -1; } duplicates = r.nextBoolean(); boltMockStatic = mock(IRichBolt.class); when(boltMockStatic.getComponentConfiguration()).thenReturn(boltConfig); checker = new TimestampOrderChecker(boltMockStatic, tsIndex, duplicates); }
From source file:de.hub.cs.dbis.aeolus.utils.TimestampMergerTest.java
@BeforeClass public static void prepareStatic() { // final long seed = System.currentTimeMillis(); final long seed = 1432597577997L; Random r = new Random(seed); System.out.println("Static test seed: " + seed); tsIndex = r.nextInt();// ww w.j a v a 2s .c o m if (tsIndex < 0) { tsIndex *= -1; } duplicates = r.nextBoolean(); boltMockStatic = mock(IRichBolt.class); when(boltMockStatic.getComponentConfiguration()).thenReturn(boltConfig); checker = new TimestampOrderChecker(boltMockStatic, tsIndex, duplicates); }
From source file:org.grouplens.lenskit.scored.RandomScoredIdListTest.java
@Parameterized.Parameters public static Collection<Object[]> data() { Random rng = new Random(); List<Object[]> idLists = Lists.newArrayListWithCapacity(TEST_COUNT); for (int test = 0; test < TEST_COUNT; test++) { int size = rng.nextInt(50); ImmutableList.Builder<ScoredId> ids = ImmutableList.builder(); for (int i = 0; i < size; i++) { double v = rng.nextGaussian() + Math.PI; ScoredIdBuilder bld = new ScoredIdBuilder(i, v); if (rng.nextBoolean()) { bld.addChannel(VAL_SYM, Math.log(v)); }//from ww w .ja va 2s . c om if (rng.nextBoolean()) { bld.addChannel(STR_SYM, Double.toString(v)); } ScoredId id = bld.build(); ids.add(id); } idLists.add(new Object[] { ids.build() }); } return idLists; }