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 getRandomNumber() {
    Random random = new Random();
    int num = random.nextInt(100);
    return num;/*from   www .ja  va 2s. co m*/
}

From source file:Main.java

private static String getOutTradeNo() {
    SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
    Date date = new Date();
    String key = format.format(date);

    Random r = new Random();
    key = key + r.nextInt();//w  ww .j  a v  a2 s  .c o m
    key = key.substring(0, 15);
    return key;
}

From source file:Main.java

public synchronized static String getOutTradeNo() {
    SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
    Date date = new Date();
    String key = format.format(date);
    Random r = new Random();
    key = key + r.nextInt();/* w w w .  j  av  a2  s . co  m*/
    key = key.substring(0, 15);
    return key;
}

From source file:Main.java

private static void printPassword() {
    String UPPER = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
    String LOWER = "abcdefghijklmnpqrstuvwxyz";
    String NUMBER = "123456789";
    String SPECIAL = "!@#$%&*+?";
    Random randGen = new Random();

    StringBuffer buf = new StringBuffer();
    buf.append(LOWER.charAt(Math.abs(randGen.nextInt()) % LOWER.length()));
    buf.append(LOWER.charAt(Math.abs(randGen.nextInt()) % LOWER.length()));
    buf.append(NUMBER.charAt(Math.abs(randGen.nextInt()) % NUMBER.length()));
    for (int i = 0; i <= 4; i++) {
        buf.append(LOWER.charAt(Math.abs(randGen.nextInt()) % LOWER.length()));
    }/* w  w  w  . j  a va2s . co  m*/
    buf.append(UPPER.charAt(Math.abs(randGen.nextInt()) % UPPER.length()));
    buf.append(LOWER.charAt(Math.abs(randGen.nextInt()) % LOWER.length()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static String getFileName(String suffix) {
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat dateFormat = new SimpleDateFormat("'xc'_yyyyMMddHHmmss", Locale.CHINESE);
    String randomNumber = "_" + new Random().nextInt(10000);
    return dateFormat.format(date) + randomNumber + suffix;
}

From source file:Main.java

public static String getRandomFileName() {
    String rel = "";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    Date curDate = new Date(System.currentTimeMillis());
    rel = formatter.format(curDate);/*  w  w  w  .j av  a  2  s .  com*/
    rel = rel + new Random().nextInt(1000);
    return rel;
}

From source file:Main.java

/**
 * Get a random color//from ww w. j  a va  2s  . c o  m
 *
 * @return Random color
 */
public static int getRandomColor() {
    Random random = new Random();
    return Color.rgb(random.nextInt(sColor_Range), random.nextInt(sColor_Range), random.nextInt(sColor_Range));
}

From source file:Main.java

public static String generateBoundary() {
    StringBuilder buffer = new StringBuilder();
    Random rand = new Random();
    int count = rand.nextInt(11) + 30; // a random size from 30 to 40
    for (int i = 0; i < count; i++) {
        buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
    }/*from w w  w. jav a2  s  .  c om*/
    return buffer.toString();
}

From source file:Main.java

/**
 * Generate a unicast MAC address./*from w ww .  j  av  a2  s  .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;
}

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 ava2s.c o m
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;
}