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(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:iddb.core.util.PasswordUtils.java

public static String getRandomString() {
    int length = 10;
    Random rand = new Random(System.currentTimeMillis());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int pos = rand.nextInt(charset.length());
        sb.append(charset.charAt(pos));/*from  ww w . ja  v  a  2 s .co  m*/
    }
    return sb.toString();
}

From source file:io.manasobi.utils.RandomUtils.java

/**
 * ,  ? ?? ? ?? .<br><br>// ww w.  java 2s.c  o  m
 *
 * RandomUtils.getString(10, 15) = "jRTwRnLzSsOWC"
 *
 * @param minSize  ?
 * @param maxSize  ?
 * @return ,  ? ?? ? ?
 */
public static String getString(int minSize, int maxSize) {
    Random generator = new Random(System.currentTimeMillis());
    int randomLength = generator.nextInt(maxSize - minSize) + minSize;

    return randomAlphabetic(randomLength);
}

From source file:io.seldon.memcache.SecurityHashPeer.java

public static synchronized String getNewId() {
    if ((prng != null) && (sha != null)) {
        //generate a random number
        String randomNum = new Integer(prng.nextInt()).toString();
        byte[] result = sha.digest(randomNum.getBytes());

        return hexEncode(result);
    } else {/*from w w  w. ja va  2 s  .  c om*/
        logger.warn("Returning non-secure possibly non-unqiue random identifier");
        Random generator = new Random();
        String randomKey = "" + generator.nextInt(99999999);
        return hexEncode(randomKey.getBytes());
    }
}

From source file:com.shaie.solr.SplitShardTest.java

private static String generateRandomBody(Random random) {
    final int numWords = random.nextInt(900) + 100;
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numWords; i++) {
        final int sentenceLength = random.nextInt(10) + 10;
        for (int j = 0; j < sentenceLength && i < numWords; j++) {
            final int wordLength = random.nextInt(7) + 3;
            sb.append(RandomStringUtils.randomAlphabetic(wordLength)).append(" ");
        }//  w  ww.j ava  2  s. co m
        sb.setLength(sb.length() - 1);
        sb.append('.');
    }
    return sb.toString();
}

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

/**
 * Makes a random cache entry.// w w w.  j av a2s  .c o m
 * @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:io.viewserver.core.Utils.java

private static byte[] getBytes(byte[] input) {
    Random random = new Random(seed);
    int length = input.length;
    byte[] output = new byte[length];
    for (int i = 0; i < length; i++) {
        output[i] = (byte) (input[i] ^ random.nextInt(256));
    }//from www.j  a v a  2  s .  co m
    return output;
}

From source file:io.github.jeddict.orm.generator.compiler.HashcodeMethodSnippet.java

private static int generatePrimeNumber(int lowerLimit, int higherLimit) {
    if (randomNumber > 0) {
        return randomNumber;
    }/* ww  w.  ja v  a2s  .  c om*/

    Random r = new Random(System.currentTimeMillis());
    int proposed = r.nextInt(higherLimit - lowerLimit) + lowerLimit;
    while (!isPrimeNumber(proposed)) {
        proposed++;
    }
    if (proposed > higherLimit) {
        proposed--;
        while (!isPrimeNumber(proposed)) {
            proposed--;
        }
    }
    return proposed;
}

From source file:de.inpiraten.jdemocrator.TAN.generator.TANGenerator.java

private static void shuffle(TAN[] array) {
    Random r = new Random();
    for (int i = array.length - 1; i >= 0; i--) {
        int j = r.nextInt(i + 1);
        TAN h = array[j];//from  w  w  w  .j ava 2  s . c o m
        array[j] = array[i];
        array[i] = h;
    }
}

From source file:com.fengduo.bee.commons.core.utils.Identities.java

/**
 * ??n?/*from w ww . j  a v a2 s  .  c  o m*/
 */
public static String randomNum(int n) {
    Random r = new Random(System.currentTimeMillis());
    StringBuffer buf = new StringBuffer(n);
    for (int i = 0; i < n; i++) {
        buf.append(r.nextInt(10));
    }
    return buf.toString();
}

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 *///w w w. j  a va  2s . c  om
private static <K, V> Map.Entry<K, V> getRandomElement(final boolean remove, final Random random,
        final Map<K, V> map) {
    if (map.isEmpty())
        throw new IllegalArgumentException("map is empty!");
    final int index = random.nextInt(map.size());

    final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
    int i = 0;

    while (i++ < index)
        it.next();

    final Map.Entry<K, V> elem = it.next();
    if (remove)
        it.remove();
    return elem;
}