62,615
社区成员
发帖
与我相关
我的任务
分享
/**
* 0~9 a~z A~Z 随机字符串生成器
*
* @param scale
* 字符串长度
* @param count
* 字符串数量
* @return 结果集合
* @date 2020-03-28 19:38:07
*/
public static List<String> RandomStr(int scale, int count)
{
StringBuilder sb = new StringBuilder();
String strNum = "0123456789";
String strCh = "abcdefghijklmnopqrstuvwxyz";
sb.append(strNum).append(strCh).append(strCh.toUpperCase());// 0~9 a~z A~Z
List<String> res = new ArrayList<>();
int index = 0;
char[] ch = new char[scale];
while (0 != count--)
{
for (int i = 0; i < ch.length; i++)
{
index = (int) (Math.random() * sb.length());// 生成0 到 sb.length()-1的随机数
ch[i] = sb.charAt(index);
}
res.add(String.valueOf(ch));
}
return res;// 大小为count
}
System.out.println(RandomStr(6, 10));
// [uY73SU, 1etcqK, WsBbT6, 5w73l3, dB5Zz1, mJQaHA, 4r1nBy, KKDLLZ, NKHMjR, KwjfW0]
int[] arr = new int[10];
int res = 0;
for (int i = 0; i < arr.length; i++)
{
res = 100000 + (int) (Math.random() * 900000);
arr[i] = res;
}
Random rand = new Random();
for(int i=0; i<10; i++) {
System.out.print(rand.nextInt(1000000) + " ");
}
记得结帖哦