如何在JAVA中随机产生10位不可重复字串的方法
2017-06-06
展开全部
建立一个char[10],然后依次随机产生一个字符串
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
/**
* 生成不重复随机字符串包括字母数字
*
* @param len
* @return
*/
public static String generateRandomStr(int len) {
//字符源,可以根据需要删减
String generateSource = "0123456789abcdefghigklmnopqrstuvwxyz";
String rtnStr = "";
for (int i = 0; i < len; i++) {
//循环随机获得当次字符,并移走选出的字符
String nowStr = String.valueOf(generateSource.charAt((int) <a href="https://www.baidu.com/s?wd=Math.floor&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9uhnkrHbvnWbkuHmduHKb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0LPj6zrHn4PW0kPWnzPjn3r0" target="_blank" class="baidu-highlight">Math.floor</a>(<a href="https://www.baidu.com/s?wd=Math.random&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9uhnkrHbvnWbkuHmduHKb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0LPj6zrHn4PW0kPWnzPjn3r0"target="_blank" class="baidu-highlight">Math.random</a>() * generateSource.length())));
rtnStr += nowStr;
generateSource = generateSource.replaceAll(nowStr, "");
}
return rtnStr;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(generateRandomStr(8));
}
}
* 生成不重复随机字符串包括字母数字
*
* @param len
* @return
*/
public static String generateRandomStr(int len) {
//字符源,可以根据需要删减
String generateSource = "0123456789abcdefghigklmnopqrstuvwxyz";
String rtnStr = "";
for (int i = 0; i < len; i++) {
//循环随机获得当次字符,并移走选出的字符
String nowStr = String.valueOf(generateSource.charAt((int) <a href="https://www.baidu.com/s?wd=Math.floor&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9uhnkrHbvnWbkuHmduHKb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0LPj6zrHn4PW0kPWnzPjn3r0" target="_blank" class="baidu-highlight">Math.floor</a>(<a href="https://www.baidu.com/s?wd=Math.random&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9uhnkrHbvnWbkuHmduHKb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0LPj6zrHn4PW0kPWnzPjn3r0"target="_blank" class="baidu-highlight">Math.random</a>() * generateSource.length())));
rtnStr += nowStr;
generateSource = generateSource.replaceAll(nowStr, "");
}
return rtnStr;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(generateRandomStr(8));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询