首页 > 其他 > 详细

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

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

/**
* 创建指定数量的随机字符串
*
* @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;
}

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!