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

public static String getRandomString(int length) {
    if (length <= 0)
        return null;

    StringBuffer sb = new StringBuffer();
    Random r = new Random();
    int range = RANDOM_CHARS.length();
    for (int i = 0; i < length; i++) {
        sb.append(RANDOM_CHARS.charAt(r.nextInt(range)));
    }//  w ww.ja  va 2s .  c  o m
    return sb.toString();
}

From source file:Main.java

public static String generateRandomString(final int len) {
    final String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    final Random random = new Random();
    final StringBuffer sb = new StringBuffer();
    for (int i = 0; i < len; i++) {
        final int number = random.nextInt(base.length());
        sb.append(base.charAt(number));//from   w w w.ja  v  a 2  s .c  o  m
    }
    return sb.toString();
}

From source file:Main.java

private static String generateString(Random rng, String characters, int length) {
    char[] text = new char[length];
    for (int i = 0; i < length; i++) {
        text[i] = characters.charAt(rng.nextInt(characters.length()));
    }//from  ww  w  .java  2 s.c  o m
    return new String(text);
}

From source file:net.dovemq.transport.link.LinkTestUtils.java

public static void sendMessagesOnLink(CAMQPLinkSender linkSender, int numMessagesToSend) {
    Random randomGenerator = new Random();
    for (int i = 0; i < numMessagesToSend; i++) {
        int randomInt = randomGenerator.nextInt(5);
        CAMQPMessage message = createMessage(randomGenerator);
        linkSender.sendMessage(new CAMQPMessage(message.getDeliveryTag(), message.getPayload()));
        try {/* w  w w .  j  a va2  s  .c  o m*/
            Thread.sleep(randomInt);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

From source file:Main.java

public static String genRandomSeq(int length) {
    String val = "";

    Random random = new Random();
    for (int i = 0; i < length; i++) {
        // Determine character or digit
        String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";

        if ("char".equalsIgnoreCase(charOrNum)) {
            int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
            val += (char) (choice + random.nextInt(26));
        } else if ("num".equalsIgnoreCase(charOrNum)) {
            val += String.valueOf(random.nextInt(10));
        }/*from   www. j a v  a  2s. c  om*/
    }

    return val;
}

From source file:Main.java

public static long getRandomLong4() {
    Random rd = new Random();
    long l2 = 0;//from  www . j a  v  a 2 s .  c o  m
    for (int i = 1; i < 100; i++) {
        long l1 = (int) (Math.random() * 9000 + 1000);
        l2 = rd.nextInt(9000) + 1000;
    }
    return l2;
}

From source file:Main.java

/**
 * Generates an array of random colors given the number of colors to create.
 * @param numOfColors, the number of colors to create.
 * @return an array of the random colors.
 *///from   w  w  w .j  a  v a  2s  .c  om
public static int[] getRandomColors(int numOfColors) {
    Random random = new Random();
    int colors[] = new int[numOfColors];
    for (int i = 0; i < numOfColors; i++) {
        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);

        if ((r + g + b) > 450) {
            r = 110;
            b = 110;
            g = 110;
        }
        colors[i] = Color.rgb(r, g, b);
    }
    return colors;
}

From source file:Main.java

protected static String generateRandomString(int demoLength) {
    String base = "abcdefghijklmnopqrstuvwxyz";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < demoLength; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));/* w w  w  .j a  v  a2s . com*/
    }
    return sb.toString();
}

From source file:org.sharetask.utility.SecurityUtil.java

/**
 * Generate password from alpha numerics characters in total length 8 characters. 
 * @return//from w  w w  .  j  av  a 2  s .  c o  m
 */
public static String generatePassword() {
    final char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    final StringBuilder sb = new StringBuilder();
    final Random random = new Random();

    for (int i = 0; i < PASSWORD_LENGT; i++) {
        final char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    return sb.toString();
}

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

public static String RandomNum(int length) {
    String str = "0123456789";
    Random random = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int num = random.nextInt(10);
        buf.append(str.charAt(num));//from  w ww .j av  a2  s  .  c o m
    }
    return buf.toString();
}