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

/**
 * Scramble oder of elements in a list./*from   w w  w . j a v  a  2 s  .c om*/
 *
 * @param list
 */
public static <E> void scrambleList(List<E> list) {
    if (isNotEmpty(list)) {
        Random generator = new Random();
        for (int i = 0; i < list.size(); i++) {
            int random = generator.nextInt(list.size());
            if (random != i) {
                //Util of vs.(swap)
                E objTemp = list.get(i);
                list.set(i, list.get(random));
                list.set(random, objTemp);
            }
        }
    }
}

From source file:com.mmj.app.common.util.SerialNumGenerator.java

/** ? */
public static String RandomString(int length) {
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int num = random.nextInt(62);
        buf.append(str.charAt(num));//from w  w w  .j a  v a 2s . c  om
    }
    return buf.toString();
}

From source file:com.mmj.app.common.util.SerialNumGenerator.java

private static String getSuffix(Long id) {
    Long num = 1l;//from w  ww.j  ava 2 s .c om
    if (id != null) {
        num = id % 100000;// id??
    }

    Random random = new Random();
    StringBuffer suffixBuffer = new StringBuffer();
    suffixBuffer.append(String.valueOf(num)).append(random.nextInt(1000));
    return StringUtils.leftPad(suffixBuffer.toString(), 8, '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

public static int[] getMachineSelectionNumNo(int size, int numbers[]) {
    int length = numbers.length;
    int num[] = new int[size];
    Random random = new Random();
    List<Integer> list = new ArrayList<Integer>();
    while (list.size() < size) {
        int number = numbers[random.nextInt(length)];
        list.add(number);// w  w  w.  j a va 2s .c o m
    }

    for (int i = 0; i < list.size(); i++) {
        num[i] = list.get(i);
    }
    return num;
}

From source file:Main.java

/**
 * Using like subList() but it take randoms elements.
 *
 * @param list/* w w  w. j  a v  a2s  .com*/
 * @param sizeOfSubList
 */
public static <E> void randomSubList(List<E> list, int sizeOfSubList) {
    List<E> subList = Collections.emptyList();
    if (isNotEmpty(list) && list.size() > sizeOfSubList) {
        subList = new ArrayList<E>(sizeOfSubList);
        Random generator = new Random();
        for (int i = 0; i < sizeOfSubList; i++) {
            int random = generator.nextInt(list.size());
            subList.add(list.get(random));
            list.remove(random);
        }
    }
    list.clear();
    list.addAll(subList);
}

From source file:com.joyent.manta.http.ShufflingDnsResolver.java

/**
 * Shuffles an array of addresses that were returned from a DNS query.
 * Shuffle algorithm inspired by <a href="http://stackoverflow.com/a/1520212/33611">this stackoverflow post.</a>
 * @param addresses addresses to shuffle
 *///from   www. j  av a2 s . co  m
private static void shuffle(final InetAddress[] addresses) {
    // Only shuffle if we have 2 or more addresses
    if (addresses.length < 2) {
        return;
    }

    Random random = ThreadLocalRandom.current();

    for (int i = addresses.length - 1; i > 0; i--) {
        int index = random.nextInt(i + 1);
        // Simple swap
        InetAddress a = addresses[index];
        addresses[index] = addresses[i];
        addresses[i] = a;
    }
}

From source file:Main.java

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value//from  w  ww  .  j  av  a 2s  . c  om
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
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

public static String getRandomString(int length) {
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(3);
        long result = 0;
        switch (number) {
        case 0:/*from  www  .  ja  v a2  s .  c om*/
            result = Math.round(Math.random() * 25 + 65);
            sb.append(String.valueOf((char) result));
            break;
        case 1:
            result = Math.round(Math.random() * 25 + 97);
            sb.append(String.valueOf((char) result));
            break;
        case 2:
            sb.append(String.valueOf(new Random().nextInt(10)));
            break;
        }

    }
    return sb.toString();
}

From source file:de.onyxbits.raccoon.gui.Traits.java

protected static String saat(Random r) {
    byte[] b = { (byte) (r.nextInt(25) + 97), (byte) (r.nextInt(25) + 97), (byte) (r.nextInt(25) + 97),
            (byte) (r.nextInt(25) + 97), };
    return new String(b);
}