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:Clases.Encriptar.java

public static String getCadenaAleatoria(int longitud) {//microtime
    String cadenaAleatoria = "";
    long milis = new java.util.GregorianCalendar().getTimeInMillis();
    Random r = new Random(milis);
    int i = 0;/*from   w  w  w .  j  a va  2s .c  o m*/
    while (i < longitud) {
        char c = (char) r.nextInt(255);
        if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '$')
                || (c == '_')) {
            cadenaAleatoria += c;
            i++;
        }
    }
    return cadenaAleatoria;
}

From source file:Main.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.  ja v  a2  s  .co m*/
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Generate array of Number//from   w  w  w .ja v a2  s.co  m
 * @param length int
 * @return String
 */
public static String generateNumber(int length) {

    String numbers = "0123456789";
    String randomNumber = "";
    Random random = new Random();
    for (int i = 0; i < length; i++) {

        randomNumber = randomNumber + numbers.charAt(random.nextInt(numbers.length()));

    }

    return randomNumber;
}

From source file:cz.cuni.mff.d3s.spl.utils.StatisticsUtils.java

/** Bootstrap from already known values.
 * /*  w  ww .j  a v a 2  s .c om*/
 * <p>
 * The bootstrapping procedure is simple as we randomly select values from
 * the source array to store in the destination one.
 * Obviously, the individual values can be repeated in the bootstrapped
 * array.
 * 
 * @param source Array with original values to bootstrap from.
 * @param dest Array where to store the bootstrapped values.
 * @param rnd Random number generator to use.
 */
public static void bootstrap(double[] source, double dest[], Random rnd) {
    for (int i = 0; i < dest.length; i++) {
        dest[i] = source[rnd.nextInt(source.length)];
    }
}

From source file:Main.java

public static String getRandomString(int length) {
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(62);
        sb.append(str.charAt(number));/*from   ww  w  .  j  a v  a 2s. c o  m*/
    }
    return sb.toString();
}

From source file:com.splunk.shuttl.testutil.TUtilsMockito.java

/**
 * @return a fairly random HttpStatus//from  w  ww  . j a v a2s  .  c o  m
 */
private static Integer getAnyHttpStatus() {
    Integer[] someHttpStatuses = new Integer[] { HttpStatus.SC_ACCEPTED, HttpStatus.SC_GATEWAY_TIMEOUT,
            HttpStatus.SC_OK, HttpStatus.SC_INTERNAL_SERVER_ERROR };
    Random rand = new Random();
    return someHttpStatuses[rand.nextInt(someHttpStatuses.length)];
}

From source file:Main.java

/**
 * Generate array of alphabet/*from  w ww . j a  v a 2s . c o  m*/
 * @return String
 */
public static String generateAlphabet(int length) {

    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    String randomAlphabet = "";
    Random random = new Random();
    for (int i = 0; i < length; i++) {

        randomAlphabet = randomAlphabet + alphabet.charAt(random.nextInt(alphabet.length()));

    }

    return randomAlphabet;
}

From source file:Main.java

public static String getRandomString(int length) {
    String str = "abcdefghigklmnopkrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(62);//0~61
        sf.append(str.charAt(number));/*  w  ww. j  a va  2s  . c  o  m*/

    }
    return sf.toString();
}

From source file:Main.java

public static String RandomNIntforStr(int digits) {
    Random random = new Random();
    StringBuilder sb = new StringBuilder(digits);
    for (int i = 0; i < digits; i++) {
        sb.append(random.nextInt(10));
        //         sb.append((char)('0' + rnd.nextInt(10)));  
    }//  w  w w  .  j  a  v  a 2s .  c om
    return sb.toString();
}

From source file:Main.java

/**
 * Generate a unicast MAC address./*  w ww .j  av a  2s.c  o  m*/
 * A unicast MAC address is the one with an even second hex.
 * i.e. x[0,2,4,6,8,A,C,E]:xx:xx:xx:xx:xx
 * 
 * @return Unicast MAC address
 */
public static String generateRandomMACAddress() {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    sb.append(Integer.toHexString(r.nextInt(16)));
    int i = r.nextInt(16);
    while (i % 2 != 0)
        i = r.nextInt(16);
    sb.append(Integer.toHexString(i));
    while (sb.length() <= 12)
        sb.append(Integer.toHexString(r.nextInt()));
    String address = prepareMACAddress(sb.subSequence(0, 12).toString());
    if (address.equals("ff:ff:ff:ff:ff:ff"))
        return generateRandomMACAddress();
    return address;
}