Example usage for java.util Random nextLong

List of usage examples for java.util Random nextLong

Introduction

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

Prototype

public long nextLong() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

Usage

From source file:com.pivotal.gemfire.tools.pulse.testbed.PropMockDataUpdater.java

private Client initMemberClient(int count, String host) {

    Client memberClient = new Client();
    Random r = new Random(System.currentTimeMillis());
    memberClient.setName("Name_" + count);
    long processCpuTime = (long) (r.nextDouble() * 100);
    memberClient.setProcessCpuTime(processCpuTime);
    memberClient.setCpuUsage(0);/* w  w  w .j  av  a  2  s. co m*/
    memberClient.setGets(Math.abs(r.nextInt(100)));
    memberClient.setHost(host);
    memberClient.setId(String.valueOf(1000 + count));
    memberClient.setPuts(Math.abs(r.nextInt(100)));
    memberClient.setCpus(Math.abs(r.nextInt(20)));
    memberClient.setQueueSize(Math.abs(r.nextInt(100)));
    if ((count % 2) == 0) {
        memberClient.setStatus("up");
    } else {
        memberClient.setStatus("down");
    }
    memberClient.setThreads(Math.abs(r.nextInt(100)));
    memberClient.setUptime(Math.abs(System.currentTimeMillis() - r.nextLong()));

    return memberClient;
}

From source file:org.apache.hadoop.hbase.master.TestRegionStateOnMasterFailure.java

/**
 * Writes some data to the table./*www  . j a v a 2s .c  o  m*/
 * @throws InterruptedException
 */
private void fillTable() throws IOException, InterruptedException {
    Random rand = new Random(19387129L);
    HTable table = testUtil.createTable(TABLE_NAME, FAMILIES);
    for (int iStoreFile = 0; iStoreFile < 4; ++iStoreFile) {
        for (int iRow = 0; iRow < 100; ++iRow) {
            final byte[] row = Bytes.toBytes("row" + iRow);
            Put put = new Put(row);
            Delete del = new Delete(row);
            for (int iCol = 0; iCol < 10; ++iCol) {
                final byte[] cf = rand.nextBoolean() ? CF1 : CF2;
                final long ts = rand.nextInt();
                final byte[] qual = Bytes.toBytes("col" + iCol);
                if (rand.nextBoolean()) {
                    final byte[] value = Bytes
                            .toBytes("value_for_row_" + iRow + "_cf_" + Bytes.toStringBinary(cf) + "_col_"
                                    + iCol + "_ts_" + ts + "_random_" + rand.nextLong());
                    put.add(cf, qual, ts, value);
                } else if (rand.nextDouble() < 0.8) {
                    del.deleteColumn(cf, qual, ts);
                } else {
                    del.deleteColumns(cf, qual, ts);
                }
            }
            table.put(put);
            table.delete(del);
            table.flushCommits();
        }
    }
    testUtil.waitUntilAllRegionsAssigned(1);
}

From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulatorParserHandler.java

long nextLong(Random rng, long n) {
    if (n <= 0)
        throw new IllegalArgumentException("n must be positive");

    long bits, val;
    do {/*from  w  ww . ja  v  a2  s. co m*/
        bits = (rng.nextLong() << 1) >>> 1;
        val = bits % n;
    } while (bits - val + (n - 1) < 0L);
    return val;
}

From source file:org.apache.hadoop.hbase.io.hfile.TestHFileBlock.java

static int writeTestKeyValues(HFileBlock.Writer hbw, int seed, boolean includesMemstoreTS, boolean useTag)
        throws IOException {
    List<KeyValue> keyValues = new ArrayList<KeyValue>();
    Random randomizer = new Random(42l + seed); // just any fixed number

    // generate keyValues
    for (int i = 0; i < NUM_KEYVALUES; ++i) {
        byte[] row;
        long timestamp;
        byte[] family;
        byte[] qualifier;
        byte[] value;

        // generate it or repeat, it should compress well
        if (0 < i && randomizer.nextFloat() < CHANCE_TO_REPEAT) {
            row = keyValues.get(randomizer.nextInt(keyValues.size())).getRow();
        } else {//from  w w w .j  a  va2 s.  c  o m
            row = new byte[FIELD_LENGTH];
            randomizer.nextBytes(row);
        }
        if (0 == i) {
            family = new byte[FIELD_LENGTH];
            randomizer.nextBytes(family);
        } else {
            family = keyValues.get(0).getFamily();
        }
        if (0 < i && randomizer.nextFloat() < CHANCE_TO_REPEAT) {
            qualifier = keyValues.get(randomizer.nextInt(keyValues.size())).getQualifier();
        } else {
            qualifier = new byte[FIELD_LENGTH];
            randomizer.nextBytes(qualifier);
        }
        if (0 < i && randomizer.nextFloat() < CHANCE_TO_REPEAT) {
            value = keyValues.get(randomizer.nextInt(keyValues.size())).getValue();
        } else {
            value = new byte[FIELD_LENGTH];
            randomizer.nextBytes(value);
        }
        if (0 < i && randomizer.nextFloat() < CHANCE_TO_REPEAT) {
            timestamp = keyValues.get(randomizer.nextInt(keyValues.size())).getTimestamp();
        } else {
            timestamp = randomizer.nextLong();
        }
        if (!useTag) {
            keyValues.add(new KeyValue(row, family, qualifier, timestamp, value));
        } else {
            keyValues.add(new KeyValue(row, family, qualifier, timestamp, value,
                    new Tag[] { new Tag((byte) 1, Bytes.toBytes("myTagVal")) }));
        }
    }

    // sort it and write to stream
    int totalSize = 0;
    Collections.sort(keyValues, KeyValue.COMPARATOR);

    for (KeyValue kv : keyValues) {
        totalSize += kv.getLength();
        if (includesMemstoreTS) {
            long memstoreTS = randomizer.nextLong();
            kv.setMvccVersion(memstoreTS);
            totalSize += WritableUtils.getVIntSize(memstoreTS);
        }
        hbw.write(kv);
    }
    return totalSize;
}

From source file:esg.node.connection.ESGConnectionManager.java

private void periodicallyRegisterToPeers() {
    log.trace("Launching connection manager's registration push timer...");
    long delay = Long.parseLong(props.getProperty("conn.mgr.initialDelay", "10"));

    final long period = Long.parseLong(props.getProperty("conn.mgr.period", "30"));
    final long slop_bounds = 15000; //represents 15000ms or 15 seconds of slop.... slop/period is the ratio of period misses
    final Random random = new Random(System.currentTimeMillis());

    log.trace("connection registration delay:  " + delay + " sec");
    log.trace("connection registration period: " + period + " sec");

    Timer timer = new Timer("Reg-Repush-Timer");

    //This will transition from active map to inactive map
    timer.schedule(new TimerTask() {
        public final void run() {
            log.debug("(Timer) Re-Pushing My Last Registry State (Event)");
            long elapsedTime = (System.currentTimeMillis()
                    - ESGConnectionManager.this.lastDispatchTime.longValue());
            long window = ((period * 1000) + (Math.abs(random.nextLong()) % slop_bounds)); //milliseconds

            log.trace("Re-push: elapsedTime=" + elapsedTime + "ms >? window=" + window + "ms");

            if (elapsedTime > window) {
                ESGConnectionManager.this.getESGEventQueue().enqueueEvent(new ESGCallableFutureEvent<Boolean>(
                        ESGConnectionManager.this, Boolean.valueOf(false), "Registration Re-push Event") {
                    public boolean call(DataNodeComponent contextComponent) {
                        log.trace("Registration Re-push \"Call\"'ed...");
                        boolean handled = false;
                        try {
                            //Note: since "Boolean" generic, setData needs to take a value of that type
                            //"handled" plays *two* roles. 1) It is the Data that is being retrieved and stored
                            //by whatever process is coded (in this case calling sendOutRegistryState()).
                            //2) It is also setting the return value for this "call" method being implemented
                            //that indicates if this call was successfully handled.  The former has its type
                            //dictated by the generic.  The latter is always a boolean.
                            setData(handled = ((ESGConnectionManager) contextComponent).sendOutRegistryState());
                        } finally {
                            log.info("Registration Re-push completed [" + handled + "]");
                        }/*  w w w . jav a2s .  c  om*/
                        return handled;
                    }
                });
            } else {
                log.debug("NOT performing re-push - last message sent approx " + (elapsedTime / 1000)
                        + "secs ago < " + (window / 1000) + "secs");
            }
        }
    }, delay * 1000, period * 1000);
}

From source file:configuration.Util.java

/**
 * Return a number containing the date and N random number
 * @return a String//ww w. j a  v a 2 s . co m
 */
public static String returnRandomAndDate() {
    Calendar today = Calendar.getInstance();
    String tmpdir = "";
    //VARIABLE
    String dd = String.valueOf(today.get(Calendar.DAY_OF_MONTH));
    String mm = String.valueOf(today.get(Calendar.MONTH));
    String yyyy = String.valueOf(today.get(Calendar.YEAR));
    Random r = new Random();
    StringBuilder s = new StringBuilder();
    s.append(yyyy);
    s.append("-");
    s.append(mm);
    s.append("-");
    s.append(dd);
    s.append("_");
    s.append(Long.toString(r.nextLong()).replaceAll("-", ""));
    return s.toString();
}

From source file:org.sakaiproject.bbb.tool.entity.BBBMeetingEntityProvider.java

/** Generate a random password */
private String generatePassword() {
    Random randomGenerator = new Random(System.currentTimeMillis());
    return Long.toHexString(randomGenerator.nextLong());
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Ignore
@Test//from   w  w  w  .j av a2  s  .c o m
public void showErrorRate() throws Exception {
    HashFunction fn = Hashing.murmur3_128();
    Random random = new Random();

    double error = 0.0d;
    int count = 0;

    final int[] valsToCheck = { 10, 20, 50, 100, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 1000000,
            2000000, 10000000, Integer.MAX_VALUE };

    for (int numThings : valsToCheck) {
        long startTime = System.currentTimeMillis();
        HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();

        for (int i = 0; i < numThings; ++i) {
            if (i != 0 && i % 100000000 == 0) {
                ++count;
                error = computeError(error, count, i, startTime, collector);
            }
            collector.add(fn.hashLong(random.nextLong()).asBytes());
        }

        ++count;
        error = computeError(error, count, numThings, startTime, collector);
    }
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testEstimation() throws Exception {
    Random random = new Random(0L);

    final int[] valsToCheck = { 10, 20, 50, 100, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 1000000,
            2000000 };/*from  w  w w.  ja va  2  s  .c  o  m*/
    final double[] expectedVals = { 11.029647221949576, 21.108407720752034, 51.64575281885815,
            100.42231726408892, 981.8579991802412, 1943.1337257462792, 4946.192042635218, 9935.088157579434,
            20366.1486889433, 49433.56029693898, 100615.26273314281, 980831.624899156000, 1982408.2608981386 };

    int valsToCheckIndex = 0;
    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    for (int i = 0; i < valsToCheck[valsToCheck.length - 1]; ++i) {
        collector.add(fn.hashLong(random.nextLong()).asBytes());
        if (i == valsToCheck[valsToCheckIndex]) {
            Assert.assertEquals(expectedVals[valsToCheckIndex], collector.estimateCardinality(), 0.0d);
            ++valsToCheckIndex;
        }
    }
    Assert.assertEquals(expectedVals.length, valsToCheckIndex + 1);
    Assert.assertEquals(expectedVals[valsToCheckIndex], collector.estimateCardinality(), 0.0d);
}

From source file:org.apache.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testEstimation() {
    Random random = new Random(0L);

    final int[] valsToCheck = { 10, 20, 50, 100, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 1000000,
            2000000 };/*from w  w  w.  ja v  a 2  s. com*/
    final double[] expectedVals = { 11.029647221949576, 21.108407720752034, 51.64575281885815,
            100.42231726408892, 981.8579991802412, 1943.1337257462792, 4946.192042635218, 9935.088157579434,
            20366.1486889433, 49433.56029693898, 100615.26273314281, 980831.624899156000, 1982408.2608981386 };

    int valsToCheckIndex = 0;
    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    for (int i = 0; i < valsToCheck[valsToCheck.length - 1]; ++i) {
        collector.add(fn.hashLong(random.nextLong()).asBytes());
        if (i == valsToCheck[valsToCheckIndex]) {
            Assert.assertEquals(expectedVals[valsToCheckIndex], collector.estimateCardinality(), 0.0d);
            ++valsToCheckIndex;
        }
    }
    Assert.assertEquals(expectedVals.length, valsToCheckIndex + 1);
    Assert.assertEquals(expectedVals[valsToCheckIndex], collector.estimateCardinality(), 0.0d);
}