Java获取随机数的代码

 1 获取随机数
 2 public static String getSerialNo(int len) {
 3         String[] baseString = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
 4                 "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
 5         String[] baseString2 = { "A", "B", "C", "D", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
 6                 "T", "U", "V", "W", "X", "Y", "Z" };
 7         Random random = new Random();
 8         int length = baseString.length;
 9         String randomString = "";
10         for (int i = 0; i < length; i++) {
11             randomString += baseString[random.nextInt(length)];
12         }
13         random = new Random(System.currentTimeMillis());
14         String resultStr = baseString2[random.nextInt(baseString2.length - 1)];
15         for (int i = 0; i < len - 1; i++) {
16             resultStr += randomString.charAt(random.nextInt(randomString.length() - 1));
17         }
18         return resultStr;
19     }

 

posted @ 2021-02-25 15:44  安宇  阅读(197)  评论(0)    收藏  举报