java随机生成字符串和校验

首先定义字符串

1 String a = "0123456789";                    // 数字
2 String b = "abcdefghijklmnopqrstuvwxyz";    // 小写字母
3 String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    // 大写字母
4 String d = "~!@#$%^&*()_+=<>/,./;'[]{}|\\"; // 特殊字符
5 String result = "";
View Code

产生随机字符串

 1 result = result + getRandomString(a, 2); // 数字
 2 result = result + getRandomString(b, 5); // 小写字母
 3 result = result + getRandomString(c, 5); // 大写字母
 4 result = result + getRandomString(d, 1); // 特殊字符
 5 
 6 /**
 7     *
 8     * @输入参数:传递一个字符串 传出字符串的长度
 9    * @输出参数:随机传出一个指定长度的子字符串
10    *
11      */
12     public static String getRandomString(String str, int length) {
13         Random random = new Random();
14         StringBuilder sb = new StringBuilder();
15         for (int i = 0; i < length; i++) {
16             int number = random.nextInt(str.length());
17             sb.append(str.charAt(number));
18         }
19         return sb.toString();
20     }
View Code

产生的字符串位置顺序随机打乱

 1 result = UnSort(result);  // 将字母顺序打乱
 2     /**
 3      *
 4      * @输入参数:传递一个字符串
 5      * @输出参数:随机将传入的子字符串乱序后输出
 6      *
 7      */
 8     public static String UnSort(String str) {
 9         List<String> lists = new ArrayList<>();
10         Random random = new Random();
11         StringBuffer sb = new StringBuffer();
12         for (int i = 0; i < str.length(); i++) {
13             lists.add(str.substring(i, i + 1));
14         }
15         while (!lists.isEmpty()) {
16             int number = random.nextInt(lists.size());
17             sb.append(lists.get(number));
18             lists.remove(number);
19         }
20         return sb.toString();
21     }
View Code

对生成的字符串生成ASCII校验尾数,这个需与与接收方进行提前约定

 1 result = ProCheck(result);
 2     /**
 3      *
 4      * @输入参数:传递一个字符串
 5      * @输出参数:在该字符串最后加上字符的ASCII累加和校验
 6      *
 7      */
 8     private String ProCheck(String str) {
 9         int sum = 0;
10         for (int i = 0; i < str.length(); i++) {
11             sum = sum + (int) str.charAt(i);
12         }
13         return str + sum;
14     }
View Code

以线程方式刷新,没隔一秒产生一个字符串可以发送出去

 1 public class MyRandom extends Thread {
 2 
 3     @Override
 4     public void run() {
 5                 while (true) {
 6                     Calendar calendar = Calendar.getInstance();  // 当前时间
 7               System.err.println(calendar.getTime() + ":" + result);
 8                     try {
 9                         sleep(1000);
10                     } catch (InterruptedException ex) {
11                             Logger.getLogger(MyRandom.class.getName()).log(Level.SEVERE, null, ex);
12             }
13         }
14 }
View Code

全部代码如下:

 1 import java.util.ArrayList;
 2 import java.util.Calendar;
 3 import java.util.List;
 4 import java.util.Random;
 5 import java.util.logging.Level;
 6 import java.util.logging.Logger;
 7 
 8 /**
 9  *
10  * @author dpchenh
11  */
12 public class MyRandom extends Thread {
13 
14     @Override
15     public void run() {
16         // 没隔一秒生成一个随机字符串:该字符串包含2个数字 5个小写字母 5个大写字母 1个特殊字符 前13位为字符位,后面位数为校验位
17         while (true) {
18             String a = "0123456789";                    // 数字
19             String b = "abcdefghijklmnopqrstuvwxyz";    // 小写字母
20             String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";    // 大写字母
21             String d = "~!@#$%^&*()_+=<>/,./;'[]{}|\\"; // 特殊字符
22             String result = "";
23             // 加密算法 获取随机数
24             result = result + getRandomString(a, 2); // 数字
25             result = result + getRandomString(b, 5); // 小写字母
26             result = result + getRandomString(c, 5); // 大写字母
27             result = result + getRandomString(d, 1); // 特殊字符
28             System.out.println("变序后sb:" + result);
29             result = UnSort(result);  // 将字母顺序打乱
30             result = ProCheck(result);
31             System.out.println("变序后sb:" + result);
32 
33             Calendar calendar = Calendar.getInstance();  // 当前时间
34             System.err.println(calendar.getTime() + ":" + result);
35             try {
36                 sleep(1000);
37             } catch (InterruptedException ex) {
38                 Logger.getLogger(MyRandom.class.getName()).log(Level.SEVERE, null, ex);
39             }
40         }
41     }
42 
43     /**
44      *
45      * @输入参数:传递一个字符串 传出字符串的长度
46      * @输出参数:随机传出一个指定长度的子字符串
47      *
48      */
49     public static String getRandomString(String str, int length) {
50         Random random = new Random();
51         StringBuilder sb = new StringBuilder();
52         for (int i = 0; i < length; i++) {
53             int number = random.nextInt(str.length());
54             sb.append(str.charAt(number));
55         }
56         return sb.toString();
57     }
58 
59     /**
60      *
61      * @输入参数:传递一个字符串
62      * @输出参数:随机将传入的子字符串乱序后输出
63      *
64      */
65     public static String UnSort(String str) {
66         List<String> lists = new ArrayList<>();
67         Random random = new Random();
68         StringBuffer sb = new StringBuffer();
69         for (int i = 0; i < str.length(); i++) {
70             lists.add(str.substring(i, i + 1));
71         }
72         while (!lists.isEmpty()) {
73             int number = random.nextInt(lists.size());
74             sb.append(lists.get(number));
75             lists.remove(number);
76         }
77         return sb.toString();
78     }
79 
80     /**
81      *
82      * @输入参数:传递一个字符串
83      * @输出参数:在该字符串最后加上字符的ASCII累加和校验
84      *
85      */
86     private String ProCheck(String str) {
87         int sum = 0;
88         for (int i = 0; i < str.length(); i++) {
89             sum = sum + (int) str.charAt(i);
90         }
91         return str + sum;
92     }
93 
94     public static void main(String[] args) {
95         MyRandom random = new MyRandom();
96         random.start();
97     }
98 }
View Code


 

猜你喜欢

转载自www.cnblogs.com/1187163927ch/p/8930080.html
今日推荐