Example usage for java.util Random nextDouble

List of usage examples for java.util Random nextDouble

Introduction

In this page you can find the example usage for java.util Random nextDouble.

Prototype

public double nextDouble() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:PowerMethod.power_method.java

public static List<RealMatrix> genMatrices() {
    double end = 2;
    double start = -2;
    List<RealMatrix> matrices = new ArrayList<>();
    Random rand = new Random();
    for (int i = 0; i < 1000; i++) {
        RealMatrix a = new Array2DRowRealMatrix(2, 2);
        a.setEntry(0, 0, rand.nextDouble() * (end - start) + start);
        a.setEntry(0, 1, rand.nextDouble() * (end - start) + start);
        a.setEntry(1, 0, rand.nextDouble() * (end - start) + start);
        a.setEntry(1, 1, rand.nextDouble() * (end - start) + start);
        matrices.add(a);/*from   w ww. ja va2 s. c o m*/
    }
    return matrices;
}

From source file:com.ricston.akka.matrix.Main.java

private static List<List<Double>> generateMatrix(int rows, int columns) {

    Random rand = new Random(System.currentTimeMillis());

    List<List<Double>> data = new ArrayList<List<Double>>(rows);

    for (int i = 0; i < rows; i++) {
        List<Double> row = new ArrayList<Double>(columns);
        for (int j = 0; j < columns; j++) {
            row.add(j, rand.nextDouble() * 20);
        }/*from   ww w  .  j  av  a  2 s .c om*/
        data.add(i, row);
    }

    return data;
}

From source file:com.google.developers.gdgfirenze.mockep.AndroidSimulator.java

private static JSONObject createJsonUpdatePacket() throws JSONException {

    Random rand = new Random();

    JSONObject root = new JSONObject();

    JSONObject obj = new JSONObject();
    root.put("sample", obj);

    obj.put("device_id", "urn:rixf:org.android/sensor_id");
    obj.put("time", dateFormat.format(new Date()));
    obj.put("battery_level", 10.0 + rand.nextDouble() * 20.0);

    JSONObject positionObj = new JSONObject();

    positionObj.put("lat", 42.5 + rand.nextDouble());
    positionObj.put("lng", 10.5 + rand.nextDouble());
    positionObj.put("alt", 100.0);
    positionObj.put("time", dateFormat.format(new Date()));
    positionObj.put("accuracy", 10.0);
    positionObj.put("bearing", 360.0 * rand.nextDouble());
    positionObj.put("speed", 0.0);

    obj.put("position", positionObj);

    JSONArray scanresultsObj = new JSONArray();
    JSONObject scanObj = new JSONObject();

    scanObj.put("frequency", 2400.0);
    scanObj.put("level", -10.0 - 20.0 * rand.nextDouble());
    scanObj.put("bssid", "BSSID");
    scanObj.put("capabilities", "[]");
    scanObj.put("ssid", "SSID");

    scanresultsObj.put(scanObj);/*w  ww .  ja v  a  2  s  . co  m*/

    obj.put("wifi_scans", scanresultsObj);

    return root;
}

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   ww w  .j av a2 s. c  o  m*/
        counter++;
    }
}

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);/* w w w.  j a  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:com.linkedin.pinot.core.data.readers.PinotSegmentUtil.java

private static Object generateSingleValue(Random random, FieldSpec.DataType dataType) {
    switch (dataType) {
    case INT://from w ww  .  j a  va  2 s  .  c  o m
        return Math.abs(random.nextInt());
    case LONG:
        return Math.abs(random.nextLong());
    case FLOAT:
        return Math.abs(random.nextFloat());
    case DOUBLE:
        return Math.abs(random.nextDouble());
    case STRING:
        return RandomStringUtils.randomAlphabetic(DEFAULT_STRING_VALUE_LENGTH);
    default:
        throw new IllegalStateException("Illegal data type");
    }
}

From source file:com.lloydtorres.stately.push.TrixHelper.java

/**
 * Sets an alarm for Alphys to query NS for new notices. The alarm time is on whatever the user
 * selected in settings, starting from the time the function was called. A "jitter" of up to
 * 5 minutes is added on top to prevent overwhelming the NS servers.
 * @param c App context/* w ww. ja va2 s .  co  m*/
 */
public static void setAlarmForAlphys(Context c) {
    // First check if alarms should be set to begin with.
    if (!SettingsActivity.getNotificationSetting(c)) {
        return;
    }

    Intent alphysIntent = new Intent(c, AlphysReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 0, alphysIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    long timeToNextAlarm = System.currentTimeMillis()
            + SettingsActivity.getNotificationIntervalSetting(c) * 1000L;
    // add "jitter" from 0 min to 5 min to next alarm to prevent overwhelming NS servers
    Random r = new Random();
    timeToNextAlarm += (long) (r.nextDouble() * FIVE_MIN_IN_MS);

    // Source:
    // https://www.reddit.com/r/Android/comments/44opi3/reddit_sync_temporarily_blocked_for_bad_api_usage/czs3ne4
    AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setExact(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    }
}

From source file:org.apache.mahout.classifier.df.data.Utils.java

/**
 * Generates a random list of tokens/*  w ww.  j a  va2 s.  c o  m*/
 * <ul>
 * <li>each attribute has 50% chance to be NUMERICAL ('N') or CATEGORICAL
 * ('C')</li>
 * <li>10% of the attributes are IGNORED ('I')</li>
 * <li>one randomly chosen attribute becomes the LABEL ('L')</li>
 * </ul>
 * 
 * @param rng Random number generator
 * @param nbTokens number of tokens to generate
 */
public static char[] randomTokens(Random rng, int nbTokens) {
    char[] result = new char[nbTokens];

    for (int token = 0; token < nbTokens; token++) {
        double rand = rng.nextDouble();
        if (rand < 0.1) {
            result[token] = 'I'; // IGNORED
        } else if (rand >= 0.5) {
            result[token] = 'C';
        } else {
            result[token] = 'N'; // NUMERICAL
        } // CATEGORICAL
    }

    // choose the label
    result[rng.nextInt(nbTokens)] = 'L';

    return result;
}

From source file:com.opengamma.bbg.test.BloombergTestUtils.java

/**
 * Creates a random tick.//from  w  w w  .  ja v a  2  s.  com
 * 
 * @param random  the source of randomness, not null
 * @param fudgeMsgFactory  the Fudge message factory, not null
 * @return the message, not null
 */
public static MutableFudgeMsg makeRandomStandardTick(Random random, FudgeMsgFactory fudgeMsgFactory) {
    MutableFudgeMsg result = fudgeMsgFactory.newMessage();
    MutableFudgeMsg bbgTickAsFudgMsg = fudgeMsgFactory.newMessage();
    bbgTickAsFudgMsg.add("BID", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("ASK", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("BEST_BID", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("BEST_ASK", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("IND_BID_FLAG", false);
    bbgTickAsFudgMsg.add("IND_ASK_FLAG", false);
    bbgTickAsFudgMsg.add("ASK_SIZE_TDY", String.valueOf(random.nextInt()));
    bbgTickAsFudgMsg.add("BID_SIZE_TDY", String.valueOf(random.nextInt()));
    bbgTickAsFudgMsg.add("BID_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("ASK_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("ASK_SIZE", String.valueOf(random.nextInt()));
    bbgTickAsFudgMsg.add("BID_SIZE", String.valueOf(random.nextInt()));
    bbgTickAsFudgMsg.add("LAST_PRICE", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("LAST_TRADE", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("VOLUME", String.valueOf(random.nextInt()));
    bbgTickAsFudgMsg.add("HIGH", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("LOW", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("OPEN", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("OPEN_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("VOLUME_TDY", "17925");
    bbgTickAsFudgMsg.add("LAST_TRADE_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("HIGH_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("LOW_TDY", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("EXCH_CODE_LAST", "D");
    bbgTickAsFudgMsg.add("LAST_PX_LOCAL_EXCH_SOURCE_RT", "UD");
    bbgTickAsFudgMsg.add("API_MACHINE", "n166");
    bbgTickAsFudgMsg.add("TRADING_DT_REALTIME", "2009-12-08+00:00");
    bbgTickAsFudgMsg.add("EQY_TURNOVER_REALTIME", "211460.515625");
    bbgTickAsFudgMsg.add("RT_API_MACHINE", "n166");
    bbgTickAsFudgMsg.add("RT_PRICING_SOURCE", "US");
    bbgTickAsFudgMsg.add("IS_DELAYED_STREAM", true);
    bbgTickAsFudgMsg.add("MKTDATA_EVENT_TYPE", "SUMMARY");
    bbgTickAsFudgMsg.add("PREV_SES_LAST_PRICE", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("RT_PX_CHG_NET_1D", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("RT_PX_CHG_PCT_1D", String.valueOf(random.nextDouble()));
    bbgTickAsFudgMsg.add("SES_START", "14:30:00.000+00:00");
    bbgTickAsFudgMsg.add("SES_END", "21:30:00.000+00:00");
    result.add(FIELDS_KEY, bbgTickAsFudgMsg);
    return result;
}

From source file:Main.java

/**
 * Samples without replacement from a collection, using your own
 * {@link Random} number generator./*from w w w .  j a v  a  2 s  .  c  o m*/
 *
 * @param c
 *          The collection to be sampled from
 * @param n
 *          The number of samples to take
 * @param r
 *          the random number generator
 * @return a new collection with the sample
 */
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {
    if (n < 0)
        throw new IllegalArgumentException("n < 0: " + n);
    if (n > c.size())
        throw new IllegalArgumentException("n > size of collection: " + n + ", " + c.size());
    List<E> copy = new ArrayList<E>(c.size());
    copy.addAll(c);
    Collection<E> result = new ArrayList<E>(n);
    for (int k = 0; k < n; k++) {
        double d = r.nextDouble();
        int x = (int) (d * copy.size());
        result.add(copy.remove(x));
    }
    return result;
}