基础工具---获取自定义长度随机数

来源:互联网 发布:mac怎么转换输入法 编辑:程序博客网 时间:2024/06/05 08:06
/***纯数字随机数*/public static String createRandom( int length) {        String retStr = "";        String strTable = "1234567890";        int len = strTable.length();        boolean bDone = true;        do {            retStr = "";            int count = 0;            for (int i = 0; i < length; i++) {                double dblR = Math.random() * len;                int intR = (int) Math.floor(dblR);                char c = strTable.charAt(intR);                if (('0' <= c) && (c <= '9')) {                    count++;                }                retStr += strTable.charAt(intR);            }            if (count >= 2) {                bDone = false;            }        } while (bDone);        return retStr;    }/***带字母的随机字符串*/public static String getRandomString(int length){        String str="abcdefghijklmnopqrstuvwxyz0123456789";        Random random=new Random();        StringBuffer sb=new StringBuffer();        for(int i=0;i<length;i++){            int number =random.nextInt(36);            sb.append(str.charAt(number));        }        return sb.toString();    }