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

public static int getRandom(int max) {
    if (random == null)
        random = new Random();
    return random.nextInt(max);
}

From source file:Main.java

public static long initRandomSerial() {
    final Random rnd = new Random();
    rnd.setSeed(System.currentTimeMillis());
    // prevent browser certificate caches, cause of doubled serial numbers
    // using 48bit random number
    long sl = ((long) rnd.nextInt()) << 32 | (rnd.nextInt() & 0xFFFFFFFFL);
    // let reserve of 16 bit for increasing, serials have to be positive
    sl = sl & 0x0000FFFFFFFFFFFFL;//w  w w .j  a v a  2 s  .  co  m
    return sl;
}

From source file:Main.java

public static Pair<Integer, Integer> randomBalanceAndAccountNumbers() {
    Random random = new Random();
    Integer balance = random.nextInt(80000);
    Integer account = 10000000 + random.nextInt(90000000);

    return Pair.create(balance, account);
}

From source file:Main.java

public static double getRandomDouble(double max) throws Exception {
    double ret;/* w w w . j  av a2s  . c  o m*/
    Random r = new Random();
    ret = r.nextDouble() * max;

    return ret;
}

From source file:Main.java

private static ByteBuffer buffer(int len) {
    byte[] b = new byte[len];
    Random r = new Random();
    r.nextBytes(b);/*  ww w. jav  a  2 s  .  c  o m*/
    return ByteBuffer.wrap(b);
}

From source file:Main.java

public static int randomRGB() {
    String r, g, b;//from   w  ww.  j a v a 2  s  .  co m
    Random random = new Random();
    r = Integer.toHexString(random.nextInt(256)).toUpperCase();
    g = Integer.toHexString(random.nextInt(256)).toUpperCase();
    b = Integer.toHexString(random.nextInt(256)).toUpperCase();
    r = r.length() == 1 ? "0" + r : r;
    g = g.length() == 1 ? "0" + g : g;
    b = b.length() == 1 ? "0" + b : b;
    return Color.parseColor("#" + (r + g + b));
}

From source file:Main.java

private static String genRandomString(int pLength) {
    Random r = new Random();
    String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < pLength; i++) {
        sb.append(letter.charAt(r.nextInt(1000000) % (letter.length())));
    }/* ww w  .j a  va  2  s  .c  o m*/
    return sb.toString();
}

From source file:Main.java

public static String generateString(int length) {
    StringBuffer sb = new StringBuffer();
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        sb.append(random.nextInt());//from  www  .j  a v a 2 s  .  c  o m
    }
    return sb.toString();
}

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 av  a 2s .  c  om
    }
    return sb.toString();
}

From source file:Main.java

/**
 *
 * @param len/*  w  w  w . j  a v  a  2 s.  c  o m*/
 * @return
 */
public static String getRandomHex(int len) {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    while (sb.length() < len) {
        sb.append(Integer.toHexString(r.nextInt()));
    }
    return sb.toString().substring(0, len).toUpperCase();
}