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:org.openmrs.util.Security.java

/**
 * This method will generate a random string
 *
 * @return a secure random token./*from   ww  w  . java2 s  . com*/
 */
public static String getRandomToken() throws APIException {
    Random rng = new Random();
    return encodeString(Long.toString(System.currentTimeMillis()) + Long.toString(rng.nextLong()));
}

From source file:org.apache.helix.taskexecution.TaskExecutionDemo.java

private static void populateDummyData(TaskResultStore taskResultStore) throws Exception {
    float fraudProbability = 0.01f;
    float clickProbability = 0.01f;
    int numImps = NUM_IMP_EVENTS;
    Random rand = new Random();
    String[] countries = { "US", "CANADA", "UK", "CHINA", "UNKNOWN" };
    String[] genders = { "M", "F", "UNKNOWN" };
    for (int i = 0; i < numImps; i++) {
        boolean isFraudulent = (rand.nextFloat() <= fraudProbability);
        String impEventId = "" + Math.abs(rand.nextLong());
        String impEvent = impEventId; // event id
        impEvent += "," + isFraudulent;
        impEvent += "," + countries[rand.nextInt(countries.length)];
        impEvent += "," + genders[rand.nextInt(genders.length)];
        taskResultStore.rpush(FilterTask.IMPRESSIONS, impEvent);

        boolean isClick = (rand.nextFloat() <= clickProbability);
        if (isClick) {
            String clickEvent = "" + Math.abs(rand.nextLong()); // event id
            isFraudulent = (rand.nextFloat() <= fraudProbability);
            clickEvent += "," + isFraudulent;
            clickEvent += "," + impEventId;
            taskResultStore.rpush(FilterTask.CLICKS, clickEvent);
        }/* www  .  j av  a2 s.c  om*/
    }
    System.out.println("Done populating dummy data");
}

From source file:edu.ku.brc.af.core.UsageTracker.java

/**
 * Gets the installation ID that 'uniquely' identifies the running instance
 * from other installations.//from  w  w w. j a v a2  s  .com
 * 
 * @return the installation ID string
 */
public synchronized static String getInstallId() {
    AppPreferences appPrefs = AppPreferences.getLocalPrefs();
    if (appPrefs.isAvailable()) {

        // get the first part of the install ID
        String installIdStart = appPrefs.get("InstallIdStart", null); //$NON-NLS-1$
        if (installIdStart == null) {
            // create a new ID start (this is the first time the app has run)
            Random r = new Random(System.currentTimeMillis());
            UUID idStart = new UUID(r.nextLong(), r.nextLong());
            installIdStart = idStart.toString();
            appPrefs.put("InstallIdStart", installIdStart); //$NON-NLS-1$
        }

        // get the last part of the install ID
        String installIdEnd = appPrefs.get("InstallIdEnd", null); //$NON-NLS-1$
        File pluginRegFile = XMLHelper.getConfigDir("plugin_registry.xml"); //$NON-NLS-1$
        long lastMod = pluginRegFile.lastModified();
        String lastModString = Long.toHexString(lastMod);

        if (installIdEnd == null || !installIdEnd.equals(lastModString)) {
            // somebody must have copied this install to a new storage
            // reset the InstallIdEnd preference
            clearUsageStats();
            appPrefs.put("InstallIdEnd", lastModString); //$NON-NLS-1$
            installIdEnd = lastModString;
        }
        String installId = installIdStart + "--" + installIdEnd; //$NON-NLS-1$
        return installId;
    }
    return null;
}

From source file:org.panthercode.arctic.core.helper.identity.Identity.java

/**
 * Generates a new identity object with given object and group name.
 * This function is <tt>synchronized</tt>.
 *
 * @param name  new object name//from  w  ww .ja va2s .c o  m
 * @param group new group name
 * @return Returns a new identity object.
 */
public static Identity generate(final String name, final String group) {
    Random seed = new Random(System.currentTimeMillis());

    HashCodeBuilder builder = new HashCodeBuilder();
    long id = builder.append(name).append(group).toHashCode();
    id = Math.abs(id * seed.nextLong());

    return new Identity(id, name, group);
}

From source file:com.qmetry.qaf.automation.util.StringUtil.java

public static String createRandomString(String prefix) {
    Random r = new Random();

    String token = Long.toString(Math.abs(r.nextLong()), 36);

    return (prefix + "_" + token);
}

From source file:com.qmetry.qaf.automation.util.StringUtil.java

public static String createRandomString() {
    Random r = new Random();

    String token = Long.toString(Math.abs(r.nextLong()), 36);

    return (token);
}

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.
 *///from  w  w  w . ja v  a2s  .  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:smartrics.rest.fitnesse.fixture.support.Tools.java

public static String makeToggleCollapseable(String message, String content) {
    Random random = new Random();
    String id = Integer.toString(content.hashCode()) + Long.toString(random.nextLong());
    StringBuffer sb = new StringBuffer();
    sb.append("<a href=\"javascript:toggleCollapsable('" + id + "');\">");
    sb.append("<img src='/files/images/collapsableClosed.gif' class='left' id='img" + id + "'/>" + message
            + "</a>");
    sb.append("<div class='hidden' id='" + id + "'>").append(content).append("</div>");
    return sb.toString();
}

From source file:nl.b3p.kaartenbalie.core.server.User.java

public static String createCode() {
    Random rd = new Random();
    StringBuilder toBeHashedString = new StringBuilder();
    toBeHashedString.append((new Date()).toString());
    toBeHashedString.append(rd.nextLong());

    byte[] hash = null;
    try {//w w  w  .  jav  a  2  s . c o m
        MessageDigest md = MessageDigest.getInstance(KBConfiguration.MD_ALGORITHM);
        md.update(toBeHashedString.toString().getBytes(KBConfiguration.CHARSET));
        hash = md.digest();
    } catch (Exception ex) {
        log.info("Can not create hash (ignoring): " + ex.getLocalizedMessage());
        hash = toBeHashedString.toString().getBytes();
    }

    return new String(Hex.encodeHex(hash));
}

From source file:Main.java

public static <T> T generateRandom(Class<T> objectClass) {
    Random r = new Random();
    if (objectClass.equals(String.class)) {
        String s = "";
        for (int i = 0; i < 10; i++) {
            char c = (char) (Math.abs(r.nextInt()) % ('Z' - 'A') + 'A');
            s = s + c;//  w ww  .  j a v  a 2 s  .co m
        }
        return objectClass.cast(s);
    } else if (objectClass.equals(Integer.class)) {
        Integer s = r.nextInt();
        return objectClass.cast(s);
    } else if (objectClass.equals(Long.class)) {
        Long s = r.nextLong();
        return objectClass.cast(s);
    } else if (objectClass.equals(java.util.Date.class)) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
        c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
        return objectClass.cast(c.getTime());
    }
    return null;
}