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:Main.java

/**
 * Fills the array with random longs.  If signed is true, negative values can be generated.
 * The values will fit within 'numberOfBits'.  This is useful for conversion tests.
 *//*from   w  w w  .  j  a va  2s  . c  o  m*/
public static void genRandomLongs(long seed, long array[], boolean signed, int numberOfBits) {
    long positiveMask = numberOfBits == 64 ? -1 : ((1l << numberOfBits) - 1);
    long negativeMask = ~positiveMask;
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        long l = r.nextLong();
        if (signed && l < 0) {
            l = l | negativeMask;
        } else {
            l = l & positiveMask;
        }
        array[i] = l;
    }
    // Seed a few special numbers we want to be sure to test.
    array[r.nextInt(array.length)] = 0l;
    array[r.nextInt(array.length)] = 1l;
    array[r.nextInt(array.length)] = positiveMask;
    if (signed) {
        array[r.nextInt(array.length)] = negativeMask;
        array[r.nextInt(array.length)] = -1;
    }
}

From source file:Main.java

public static <T extends BroadcastReceiver> void scheduleUpdate(Context context, Class<T> clazz) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, clazz);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Random random = new Random(System.currentTimeMillis());
    long offset = random.nextLong() % (12 * 60 * 60 * 1000);
    long interval = (24 * 60 * 60 * 1000) + offset;
    String prefKey = "pref_scheduled_monitor_config_update_" + clazz.getCanonicalName();
    long scheduledTime = preferences.getLong(prefKey, -1);

    if (scheduledTime == -1) {
        context.sendBroadcast(intent);//w  w w.j a va 2  s. c o  m
    }

    if (scheduledTime <= System.currentTimeMillis()) {
        context.sendBroadcast(intent);
        scheduledTime = System.currentTimeMillis() + interval;
        preferences.edit().putLong(prefKey, scheduledTime).commit();
        Log.w("PeriodicActionUtils",
                "Scheduling for all new time: " + scheduledTime + " (" + clazz.getSimpleName() + ")");
    } else {
        Log.w("PeriodicActionUtils", "Scheduling for time found in preferences: " + scheduledTime + " ("
                + clazz.getSimpleName() + ")");
    }

    am.cancel(sender);
    am.set(AlarmManager.RTC_WAKEUP, scheduledTime, sender);

    Log.w("PeriodicActionUtils", "Scheduled for: " + scheduledTime + " (" + clazz.getSimpleName() + ")");
}

From source file:Main.java

public static long nextLong(Random rng, long n) {
    // error checking and 2^x checking removed for simplicity.
    long bits, val;
    do {/*from  ww  w. j av  a 2 s  .  c o m*/
        bits = (rng.nextLong() << 1) >>> 1;
        val = bits % n;
    } while (bits - val + (n - 1) < 0L);
    return val;
}

From source file:com.dhenton9000.selenium.generic.UtilMethods.java

public static String generateRandomName(String rootName) {
    Random r = new Random();
    String randomStr = Long.toString(Math.abs(r.nextLong()), 21);
    String randomName = rootName + "_" + randomStr.substring(0, 4);
    return randomName;
}

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

public static String createXSRFToken(final HttpServletRequest request) throws IOException {
    // getSession(false) as this method never creates a new session
    HttpSession session = request.getSession(false);
    String xsrfSessionToken = (String) session.getAttribute(XSRF_TOKEN_NAME);
    if (xsrfSessionToken == null) {
        Random r = new Random(System.currentTimeMillis());
        long value = System.currentTimeMillis() + r.nextLong();
        char ids[] = session.getId().toCharArray();
        for (int i = 0; i < ids.length; i++) {
            value += ids[i] * (i + 1);/*w w w .ja  v a2s . c  o m*/
        }
        xsrfSessionToken = Long.toString(value);
        session.setAttribute(XSRF_TOKEN_NAME, xsrfSessionToken);
    }
    return xsrfSessionToken;
}

From source file:com.g414.dgen.field.Fields.java

/** returns a random long in the given range */
public static Field<Long> getLongField(final String name, final long start, final long stop) {
    return new Field<Long>() {
        private final long range = stop - start;

        @Override/*from w w  w.  j a va2s  .c o m*/
        public String getName() {
            return name;
        }

        @Override
        public Long getValue(String entityId, Map<String, Object> entitySoFar, Random random) {
            long next = Math.abs(random.nextLong() % range);

            return next + start;
        }
    };
}

From source file:lti.oauth.OAuthUtil.java

public static String generateNonce() throws Exception {
    Random rand = SecureRandom.getInstance("SHA1PRNG");
    return String.valueOf(rand.nextLong());
}

From source file:Main.java

/**
 * Fills the array with random ints.  Values will be between min (inclusive) and
 * max (inclusive).//from  www. ja va2  s.co  m
 */
public static void genRandomInts(long seed, int min, int max, int array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        long range = max - min + 1;
        array[i] = (int) (min + r.nextLong() % range);
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
}

From source file:de.fu_berlin.inf.dpp.netbeans.feedback.FileSubmitter.java

private static String generateBoundary() {
    Random random = new Random();
    return Long.toHexString(random.nextLong()) + Long.toHexString(random.nextLong())
            + Long.toHexString(random.nextLong());
}

From source file:goraci.Walker.java

private static CINode findStartNode(Random rand, DataStore<Long, CINode> store) throws IOException {
    Query<Long, CINode> query = store.newQuery();
    query.setStartKey(rand.nextLong());
    query.setLimit(1);/*from  www . ja v  a 2  s . c  om*/
    query.setFields(PREV_FIELD);

    long t1 = System.currentTimeMillis();
    Result<Long, CINode> rs = store.execute(query);
    long t2 = System.currentTimeMillis();

    if (rs.next()) {
        System.out.printf("FSR %d %016x\n", t2 - t1, rs.getKey());
        return rs.get();
    }

    System.out.println("FSR " + (t2 - t1));

    return null;
}