码迷,mamicode.com
首页 > 其他好文 > 详细

创建指定数量的随机字符串

时间:2016-12-08 18:18:04      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:tab   turn   count   private   创建   i++   mat   and   creat   

/**
* 创建指定数量的随机字符串
*
* @param numberFlag
* 是否是数字
* @param length
* @return String
*/
private static String createRandom(boolean numberFlag, int length) {
String retStr = "";
String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
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;
}

创建指定数量的随机字符串

标签:tab   turn   count   private   创建   i++   mat   and   creat   

原文地址:http://www.cnblogs.com/muliu/p/6145710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!