Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

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

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

public static int GetRandomColor() {
    Random random = new Random();
    int p = random.nextInt(Colors.length);
    while (Colors[p].equals(lastColor0) || Colors[p].equals(lastColor1) || Colors[p].equals(lastColor2)) {
        p = random.nextInt(Colors.length);
    }//from www .  j  a  va2s  .c o m
    lastColor0 = lastColor1;
    lastColor1 = lastColor2;
    lastColor2 = Colors[p];
    return Color.parseColor(Colors[p]);
}

From source file:Main.java

private static Bundle createNamePortBundle(String name, int port, TreeMap<String, ArrayList<String>> ips) {
    Bundle namePort = new Bundle();
    namePort.putString("name", name);
    namePort.putInt("port", port);
    if (ips != null) {
        ArrayList<String> ip = ips.get(name);
        Collections.shuffle(ip, new Random());
        namePort.putString("ip", ip.get(0));
    }/*w w w  .  j av  a  2  s.c o m*/
    return namePort;
}

From source file:Main.java

/**
 * Get a random double number from min to max.
 *
 * @param min//from ww  w  .  java2  s.c  om
 *            the minimum value
 * @param max
 *            the maximum value
 * @return
 */
public static double getRandomValue(double min, double max) {
    Random r = new Random();
    return ((int) (10000 * ((r.nextDouble() * (max - min)) + min))) / 10000.0;
}

From source file:Main.java

public static int randInt(int min, int max) {

    // Usually this can be a field rather than a method variable
    Random rand = new Random();

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

From source file:Main.java

private static String getRandomHost() {
    Random r = new Random();
    return BEECLOUD_HOSTS[r.nextInt(BEECLOUD_HOSTS.length)] + HOST_API_VERSION;
}

From source file:MainClass.java

private static String encrypt(char[] password, String plaintext) throws Exception {

    byte[] salt = new byte[8];
    Random random = new Random();
    random.nextBytes(salt);/*w  w w.  j a  v  a2s.co m*/

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext.getBytes());

    BASE64Encoder encoder = new BASE64Encoder();

    String saltString = encoder.encode(salt);
    String ciphertextString = encoder.encode(ciphertext);

    return saltString + ciphertextString;
}

From source file:Main.java

public static Calendar getRandomApodDate() {
    Calendar newCalendar = Calendar.getInstance();

    Calendar today = Calendar.getInstance();
    int currentYear = today.get(Calendar.YEAR);
    int currentMonth = today.get(Calendar.MONTH);
    int currentDay = today.get(Calendar.DATE);

    int firstApodYear = 1995;
    int firstApodMonth = 5;
    int firstApodDay = 20;

    Random rnd = new Random();
    int newYear = rnd.nextInt(currentYear - firstApodYear + 1) + firstApodYear;

    int minMonth = 0;
    int maxMonth = 11;
    if (newYear == firstApodYear)
        minMonth = firstApodMonth;//from   w w  w. jav a  2  s .  com
    if (newYear == currentYear)
        maxMonth = currentMonth;
    int newMonth = rnd.nextInt(maxMonth - minMonth + 1) + minMonth;

    newCalendar.set(newYear, newMonth, 1);

    int minDay = 1;
    int maxDay = newCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (newYear == firstApodYear && newMonth == firstApodMonth)
        minDay = firstApodDay;
    if (newYear == currentYear && newMonth == currentMonth)
        maxDay = currentDay;
    int newDay = rnd.nextInt(maxDay - minDay + 1) + minDay;

    newCalendar.set(newYear, newMonth, newDay);
    return newCalendar;
}

From source file:Main.java

public static int getRandomColor() {
    List<Integer> colors = new ArrayList<>();
    colors.add(Color.BLACK);//from   ww  w  . j a va 2s . c  o  m
    colors.add(Color.RED);
    colors.add(Color.GREEN);
    colors.add(Color.BLUE);
    colors.add(Color.YELLOW);
    colors.add(Color.GRAY);
    colors.add(Color.MAGENTA);
    colors.add(Color.CYAN);
    colors.add(Color.LTGRAY);
    return colors.get(new Random().nextInt(colors.size()));
}

From source file:com.android.volley.utils.CacheTestUtils.java

/**
 * Makes a random cache entry./*from   ww w. j  av  a2s.c om*/
 * @param data Data to use, or null to use random data
 * @param isExpired Whether the TTLs should be set such that this entry is expired
 * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
 */
public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.lastModified = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}

From source file:Main.java

/**
 * Samples without replacement from a collection.
 *
 * @param c//from  w w  w. jav a2s  .com
 *          The collection to be sampled from
 * @param n
 *          The number of samples to take
 * @return a new collection with the sample
 */
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {
    return sampleWithoutReplacement(c, n, new Random());
}