Example usage for java.util Random nextInt

List of usage examples for java.util Random nextInt

Introduction

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

Prototype

public int nextInt() 

Source Link

Document

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

Usage

From source file:MagjaNumberUtils.java

/**
 * Generates a random integer inside the lo and hi interval
 * @param lo/*from w w w .  j  av a  2s  . c  om*/
 * @param hi
 * @return the generated int
 */
public static int randomInteger(int lo, int hi) {
    java.util.Random rn = new java.util.Random();
    int n = hi - lo + 1;
    int i = rn.nextInt() % n;
    if (i < 0)
        i = -i;
    return lo + i;
}

From source file:Main.java

public static String getRandomStr(int len) {
    Random rd = new Random();
    String retStr = "";
    do {/*  w w w.  j ava  2  s. c om*/
        int rdGet1 = Math.abs(rd.nextInt()) % 10;

        int rdGet2 = Math.abs(rd.nextInt()) % 26 + 65;
        retStr = retStr + rdGet1 + (char) rdGet2;
    } while (retStr.length() < len);

    return retStr;
}

From source file:Main.java

static File make_tmpdir(Context context, SQLiteDatabase db) throws Exception {

    File extdir = Environment.getExternalStorageDirectory();
    File tmp_top = new File(extdir, "tmp_LongText");
    if (!tmp_top.exists()) {
        if (!tmp_top.mkdir())
            throw new Exception("cannot create directory: " + tmp_top.getPath());
    }/*from  w  w w.  j av a 2s.c om*/
    if (!tmp_top.canWrite())
        throw new Exception("missing permission to write to " + tmp_top.getPath());

    File tmpdir;
    Random r = new Random();
    do {
        tmpdir = new File(tmp_top, String.format("%d", r.nextInt()));
    } while (tmpdir.exists());
    if (!tmpdir.mkdir())
        throw new Exception("cannot create directory: " + tmp_top.getPath());
    if (!tmpdir.canWrite())
        throw new Exception("missing permission to write to " + tmp_top.getPath());
    ContentValues v = new ContentValues();
    v.put("pid", Process.myPid());
    v.put("tmpdir", tmpdir.getPath());
    v.put("ctime", System.currentTimeMillis());
    db.insert("tmpdir", null, v);

    return tmpdir;
}

From source file:Main.java

/**
 *
 * @param len//from  ww w.jav a2 s.c  om
 * @return
 */
public static String getRandomHex(int len) {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    while (sb.length() < len) {
        sb.append(Integer.toHexString(r.nextInt()));
    }
    return sb.toString().substring(0, len).toUpperCase();
}

From source file:Main.java

public static String generateRandomDate() {
    Random r = new Random();
    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);
    c.setLenient(true);//from   w w  w .j  av a 2s  . c om
    return DATE_FORMAT.format(c.getTime());
}

From source file:Main.java

public static int[] generateCodePointSet(final int codePointSetSize, final Random random) {
    final int[] codePointSet = new int[codePointSetSize];
    for (int i = codePointSet.length - 1; i >= 0;) {
        final int r = Math.abs(random.nextInt());
        if (r < 0)
            continue;
        // Don't insert 0~0x20, but insert any other code point.
        // Code points are in the range 0~0x10FFFF.
        final int candidateCodePoint = 0x20 + r % (Character.MAX_CODE_POINT - 0x20);
        // Code points between MIN_ and MAX_SURROGATE are not valid on their own.
        if (candidateCodePoint >= Character.MIN_SURROGATE && candidateCodePoint <= Character.MAX_SURROGATE)
            continue;
        codePointSet[i] = candidateCodePoint;
        --i;// ww w  . j ava 2s. co m
    }
    return codePointSet;
}

From source file:com.parse.ParsePushUnityHelper.java

/**
 * A helper method that provides default behavior for handling ParsePushNotificationReceived.
 *//*from   w w  w  .j a  v  a2 s  .  c o  m*/
public static void handleParsePushNotificationReceived(Context context, String pushPayloadString) {
    try {
        JSONObject pushData = new JSONObject(pushPayloadString);

        if (pushData == null || (!pushData.has("alert") && !pushData.has("title"))) {
            return;
        }

        ManifestInfo info = new ManifestInfo(context);
        String title = pushData.optString("title", info.getDisplayName());
        String alert = pushData.optString("alert", "Notification received.");
        String tickerText = title + ": " + alert;

        Random random = new Random();
        int contentIntentRequestCode = random.nextInt();

        Intent activityIntent = info.getLauncherIntent();
        PendingIntent pContentIntent = PendingIntent.getActivity(context, contentIntentRequestCode,
                activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentTitle(title)
                .setContentText(alert).setTicker(tickerText).setSmallIcon(info.getPushIconId())
                .setContentIntent(pContentIntent).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);

        Notification notification = builder.build();
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int notificationId = (int) System.currentTimeMillis();

        try {
            manager.notify(notificationId, notification);
        } catch (SecurityException se) {
            // Some phones throw exception for unapproved vibration.
            notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
            manager.notify(notificationId, notification);
        }
    } catch (JSONException e) {
        // Do nothing.
    }
}

From source file:Main.java

@Nonnull
public static int[] generateCodePointSet(final int codePointSetSize, @Nonnull final Random random) {
    final int[] codePointSet = new int[codePointSetSize];
    for (int i = codePointSet.length - 1; i >= 0;) {
        final int r = Math.abs(random.nextInt());
        if (r < 0) {
            continue;
        }//  www.j  a v a2  s  .  c o m
        // Don't insert 0~0x20, but insert any other code point.
        // Code points are in the range 0~0x10FFFF.
        final int candidateCodePoint = 0x20 + r % (Character.MAX_CODE_POINT - 0x20);
        // Code points between MIN_ and MAX_SURROGATE are not valid on their own.
        if (candidateCodePoint >= Character.MIN_SURROGATE && candidateCodePoint <= Character.MAX_SURROGATE) {
            continue;
        }
        codePointSet[i] = candidateCodePoint;
        --i;
    }
    return codePointSet;
}

From source file:Main.java

public static long initRandomSerial() {
    final Random rnd = new Random();
    rnd.setSeed(System.currentTimeMillis());
    // prevent browser certificate caches, cause of doubled serial numbers
    // using 48bit random number
    long sl = ((long) rnd.nextInt()) << 32 | (rnd.nextInt() & 0xFFFFFFFFL);
    // let reserve of 16 bit for increasing, serials have to be positive
    sl = sl & 0x0000FFFFFFFFFFFFL;/*from   w ww  .  j  a va 2  s. com*/
    return sl;
}

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  v a2s .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");
    }
}