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 getRandom(char[] sourceChar, int length) {
    if (sourceChar == null || sourceChar.length == 0 || length < 0) {
        return null;
    }/*from w  w w.j  a v a  2  s. c o  m*/

    StringBuilder str = new StringBuilder(length);
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        str.append(sourceChar[random.nextInt(sourceChar.length)]);
    }
    return str.toString();
}

From source file:outfox.dict.contest.test.InitSingerData.java

public static String getRandomSchool() {
    String[] arr = { "?", "", "", "", "?",
            "?", "" };
    Random rand = new Random();
    int index = rand.nextInt(arr.length);
    return arr[index];
}

From source file:Main.java

public static String getRandom(int len, String[] arr) {
    String s = "";
    if (0 < len && null != arr && arr.length >= 0) {
        Random ra = new Random();
        int arrLen = arr.length;
        for (int i = 0; i < len; i++) {
            s += arr[ra.nextInt(arrLen)];
        }/* ww w.  j  a  v a 2s . c  o  m*/
        return s;
    } else {
        return s;
    }
}

From source file:Main.java

private static String randomSSID(int n) {
    String chars = "abcdeifABCDEIF0123456789";
    String res = "";
    Random rnd = new Random();
    int leng = chars.length();
    for (int i = 0; i < n; i++) {
        res = res + chars.charAt(rnd.nextInt(leng));

    }// w w w  .  j a v a2  s  .  c  o  m
    return res;
}

From source file:Main.java

private static String randomABC(int n) {
    String chars = "abcde0123456789";
    String res = "";
    Random rnd = new Random();
    int leng = chars.length();
    for (int i = 0; i < n; i++) {
        res = res + chars.charAt(rnd.nextInt(leng));

    }/*from ww  w  . j  ava  2s.  c o  m*/
    return res;
}

From source file:Main.java

/**
 * Shuffle an array.//from  w w  w  .ja  va2 s .  co  m
 * @param array Array.
 */
private static void Shuffle(double[] array) {
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        int index = random.nextInt(i + 1);
        double temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:Main.java

/**
 * Shuffle an array.// ww  w.  j a v  a  2  s . c om
 * @param array Array.
 */
private static void Shuffle(int[] array) {
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        int index = random.nextInt(i + 1);
        int temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:Main.java

/**
 * Shuffle an array.//from w  ww. ja v a 2s .c o m
 * @param array Array.
 */
private static void Shuffle(float[] array) {
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        int index = random.nextInt(i + 1);
        float temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
}

From source file:Main.java

public static String getNonce() {
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 18; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));//from w  w  w . j a v  a2 s . co m
    }
    return sb.toString();
}

From source file:Main.java

private static byte[] getIV() {
    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 16; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));/*  w  w  w .j av  a  2 s  .  c  o  m*/
    }
    return sb.toString().getBytes();
}