List of usage examples for java.util Random nextFloat
public float nextFloat()
From source file:Main.java
/** * Fills the array with random floats. Values will be between min (inclusive) and * max (inclusive).//from ww w . j a va2 s. co m */ public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) { Random r = new Random(seed); int minExponent = Math.min(Math.getExponent(min), 0); int maxExponent = Math.max(Math.getExponent(max), 0); if (minExponent < -6 || maxExponent > 6) { // Use an exponential distribution int exponentDiff = maxExponent - minExponent; for (int i = 0; i < array.length; i++) { float mantissa = r.nextFloat(); int exponent = minExponent + r.nextInt(maxExponent - minExponent); int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1 float rand = sign * mantissa * (float) Math.pow(2.0, exponent); if (rand < min || rand > max) { continue; } array[i] = rand; } } else { // Use a linear distribution for (int i = 0; i < array.length; i++) { float rand = r.nextFloat(); array[i] = min + rand * (max - min); } } // Seed a few special numbers we want to be sure to test. for (int i = 0; i < sInterestingDoubles.length; i++) { float f = (float) sInterestingDoubles[i]; if (min <= f && f <= max) { array[r.nextInt(array.length)] = f; } } array[r.nextInt(array.length)] = min; array[r.nextInt(array.length)] = max; if (includeExtremes) { array[r.nextInt(array.length)] = Float.NaN; array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY; array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY; array[r.nextInt(array.length)] = Float.MIN_VALUE; array[r.nextInt(array.length)] = Float.MIN_NORMAL; array[r.nextInt(array.length)] = Float.MAX_VALUE; array[r.nextInt(array.length)] = -Float.MIN_VALUE; array[r.nextInt(array.length)] = -Float.MIN_NORMAL; array[r.nextInt(array.length)] = -Float.MAX_VALUE; } }
From source file:edu.berkeley.sparrow.examples.ProtoBackend.java
/** * Benchmark that runs random floating point multiplications for the specified amount of * "iterations", where each iteration is one millisecond. */// w ww . j a va2 s . c o m public static void runFloatingPointBenchmark(int iterations, Random r) { int runtimeMillis = iterations; long startTime = System.nanoTime(); int opsPerIteration = 1000; /* We keep a running result here and print it out so that the JVM doesn't * optimize all this computation away. */ float result = r.nextFloat(); while ((System.nanoTime() - startTime) / (1000.0 * 1000.0) < runtimeMillis) { for (int j = 0; j < opsPerIteration; j++) { // On each iteration, perform a floating point multiplication float x = r.nextFloat(); float y = r.nextFloat(); result += (x * y); } } LOG.debug("Benchmark result " + result); }
From source file:org.wso2.carbon.sample.performance.Client.java
private static void sendWarmUpEvents(DataPublisher dataPublisher, long warmUpCount) { long counter = 0; Random randomGenerator = new Random(); String streamId = "org.wso2.event.sensor.stream:1.0.0"; while (counter < warmUpCount) { 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, "warmup-" + counter }, new Object[] { longitude, latitude }, new Object[] { humidity, sensorValue }); dataPublisher.publish(event);/*from w w w . j a v a2 s . c o m*/ counter++; } }
From source file:hivemall.mix.server.MixServerTest.java
private static void invokeClient01(String groupId, int serverPort, boolean denseModel, boolean cancelMix) throws InterruptedException { PredictionModel model = denseModel ? new DenseModel(100, false) : new SparseModel(100, false); model.configureClock();// w ww . ja v a 2 s . c o m MixClient client = null; try { client = new MixClient(MixEventName.average, groupId, "localhost:" + serverPort, false, 3, model); model.configureMix(client, cancelMix); final Random rand = new Random(43); for (int i = 0; i < 1000000; i++) { Integer feature = Integer.valueOf(rand.nextInt(100)); float weight = rand.nextFloat() >= 0.5f ? 1.f : 0.f; model.set(feature, new WeightValue(weight)); } waitForMixed(model, 100000L, 10000L); long numMixed = model.getNumMixed(); Assert.assertTrue("number of mix events: " + numMixed, numMixed > 0); for (int i = 0; i < 100; i++) { float w = model.getWeight(i); Assert.assertEquals(0.5f, w, 0.1f); } } finally { IOUtils.closeQuietly(client); } }
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);// www .ja va 2 s . co 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:com.linkedin.pinot.core.segment.index.creator.RawIndexCreatorTest.java
/** * Helper method that generates a random value for a given data type * * @param dataType Data type for which to generate the random value * @return Random value for the data type. */// w w w . j a va 2 s.co m public static Object getRandomValue(Random random, FieldSpec.DataType dataType) { Object value; switch (dataType) { case INT: value = random.nextInt(); break; case LONG: value = random.nextLong(); break; case FLOAT: value = random.nextFloat(); break; case DOUBLE: value = random.nextDouble(); break; case STRING: value = StringUtil .trimTrailingNulls(RandomStringUtils.random(random.nextInt(MAX_STRING_LENGTH_IN_BYTES))); break; default: throw new IllegalArgumentException("Illegal data type for random value generator: " + dataType); } return value; }
From source file:com.esri.arcgis.android.samples.nearby.Nearby.java
private static String getRating() { // Randomized ratings could be replaced by a ratings service from a third // party./*from w ww . j av a 2s .c o m*/ Random r = new Random(); return String.valueOf(1 + (r.nextFloat() * 4)); }
From source file:com.rbsoftware.pfm.personalfinancemanager.utils.Utils.java
/** * Generates random alphanumeric string//from ww w. ja v a2 s . c o m * * @param length of string * @return random string */ public static String generateRandomString(int length) { String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvmxyz1234567890"; StringBuilder salt = new StringBuilder(); Random rnd = new Random(); while (salt.length() < length) { int index = (int) (rnd.nextFloat() * SALTCHARS.length()); salt.append(SALTCHARS.charAt(index)); } return salt.toString(); }
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);//www. j av a 2s . c om 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.apache.druid.segment.IndexMergerV9WithSpatialIndexTest.java
private static IncrementalIndex makeIncrementalIndex() throws IOException { IncrementalIndex theIndex = new IncrementalIndex.Builder() .setIndexSchema(/* w w w. j a va2s. c o m*/ new IncrementalIndexSchema.Builder().withMinTimestamp(DATA_INTERVAL.getStartMillis()) .withQueryGranularity(Granularities.DAY).withMetrics(METRIC_AGGS) .withDimensionsSpec(new DimensionsSpec(null, null, Arrays.asList( new SpatialDimensionSchema("dim.geo", Arrays.asList("lat", "long")), new SpatialDimensionSchema("spatialIsRad", Arrays.asList("lat2", "long2")) ))).build()).setReportParseExceptions(false).setMaxRowCount(NUM_POINTS) .buildOnheap(); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-01").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-01").toString(), "dim", "foo", "lat", 0.0f, "long", 0.0f, "val", 17L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-02").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-02").toString(), "dim", "foo", "lat", 1.0f, "long", 3.0f, "val", 29L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-03").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-03").toString(), "dim", "foo", "lat", 4.0f, "long", 2.0f, "val", 13L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-04").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-04").toString(), "dim", "foo", "lat", 7.0f, "long", 3.0f, "val", 91L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-05").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-05").toString(), "dim", "foo", "lat", 8.0f, "long", 6.0f, "val", 47L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-05").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-05").toString(), "dim", "foo", "lat", "_mmx.unknown", "long", "_mmx.unknown", "val", 101L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-05").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-05").toString(), "dim", "foo", "dim.geo", "_mmx.unknown", "val", 501L))); theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-05").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-05").toString(), "lat2", 0.0f, "long2", 0.0f, "val", 13L))); // Add a bunch of random points Random rand = ThreadLocalRandom.current(); for (int i = 8; i < NUM_POINTS; i++) { theIndex.add(new MapBasedInputRow(DateTimes.of("2013-01-01").getMillis(), DIMS, ImmutableMap.of("timestamp", DateTimes.of("2013-01-01").toString(), "dim", "boo", "lat", (float) (rand.nextFloat() * 10 + 10.0), "long", (float) (rand.nextFloat() * 10 + 10.0), "val", i))); } return theIndex; }