Java-Random-获取

/**
 * 获取随机数!
 * @param length:获取随机数长度!
 * @return StringBuffer
 * 随机数获取公式:(数据类型)(最小值+Math.random()*(最大值-最小值+1))
 *         (int)(0+Math.random()*(9-0+1));
 * 随机数获取公式:(类型)最小值+Math.random()*最大值
 *         (int)(1+Math.random()*10);
 */
public static StringBuffer getRandom(Integer length){
    StringBuffer strb = null;
    if(length>0){
        strb = new StringBuffer();
        for (int i=0;i<length;i++) {
            strb.append((int)(1+Math.random()*(9-0+1)));
        }
    }
    return strb;
}
 
/**
 * A1
 * 标准6位随机数获取;
 * @return Integer
 */
public static Integer getRandomNumFirst(){
    return (int) ((Math.random()*9+1)*100000);
}
    
/**
 * A2
 * 标准6位随机数获取;
 * @return
 */
public static Integer getRandomNumSecond(){
     Random random = new Random();
     return random.nextInt(900000)+100000; 
}
 
/**
 * A3
 * 随机生成六位数验证码!
 * @return
 */
public static Integer getRandomNumThried(){
     return (int) (Math.random()*(999999-100000)+100000); 
}
 
/**
 * B1
 * 6位随机数获取;
 * @return String
 */
public static String getRandomStr() {
    String str = "0123456789";
    str.charAt(4);
    StringBuilder sb = new StringBuilder(4);
    for (int i = 0; i < 6; i++) {
       char ch = str.charAt(new Random().nextInt(str.length()));
       sb.append(ch);
    }
    return sb.toString();
}
 
/**
 * C
 * Random随机数获取;6位之内;
 * @return Integer
 */
public static Integer getRandom(){
    Random random = new Random(); 
    return random.nextInt(1000000);
}
原文地址:https://www.cnblogs.com/tanjiyuan/p/11858717.html