Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

In this page you can find the example usage for java.util Random Random.

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

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));//ww  w.  j ava  2s.co  m
    }
    return sb.toString().getBytes();
}

From source file:Main.java

public static <T> T random(Collection<T> coll) {
    int num = (new Random().nextInt(coll.size()));
    for (T t : coll) {
        if (--num < 0) {
            return t;
        }//  w w w.  ja  v  a2s.c o m
    }

    return null;
}

From source file:Main.java

public static String generateRandomStrs(int strCount) {
    StringBuilder randomStrs = new StringBuilder();
    Random randomGenerator = new Random();
    for (int i = 1; i < strCount; i++) {
        int randomInt = randomGenerator.nextInt(26);
        randomStrs.append((char) (randomInt + 'a'));
    }//from w w  w. ja va 2  s  .  c o m

    return randomStrs.toString();
}

From source file:Main.java

public static int getRandomRoll(int min, int max) {
    Random random = new Random();
    return random.nextInt((max - min) + 1) + min;
}

From source file:Main.java

public static String get_random_relax_message() {
    int idx = new Random().nextInt(rm.length);
    return (rm[idx]);
}

From source file:Main.java

public static int[] randomIntArray(int length) {
    int[] ran = new int[length];
    Random random = new Random();
    for (int n = 0; n < ran.length; n++) {
        ran[n] = random.nextInt(100);/*from   w ww .  j  ava 2  s.co m*/
    }
    return ran;
}

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:// w w w  . j ava2s .  c o  m
            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:Main.java

/**
 * @param min/* www  . ja  va2  s . com*/
 * @param max
 * @return
 */
public static String getYZM(final int min, final int max) {
    Random random = new Random();
    int randomnum = 0;
    int tmp = Math.abs(random.nextInt());
    randomnum = tmp % (max - min + 1) + min;
    return randomnum + "";
}

From source file:Main.java

public static final String encodeInterfereWord(String src) {
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    int len = base.length();
    for (int index = 0, size = src.length(); index < size; index++) {
        sb.append(base.charAt(random.nextInt(len)));
        sb.append(src.charAt(index));// w  w  w . j  av  a 2  s  . c  o  m
    }
    sb.append(base.charAt(random.nextInt(len)));
    return sb.toString();
}

From source file:Main.java

public static int getRandom(int min, int max) {
    if (max == min) {
        min = 0;/*from   www  . j  ava  2 s .c  om*/
        max = 100;
    }
    Random random = new Random();

    return random.nextInt(max) % (max - min + 1) + min;
}