java提供了Math.random()函數(shù),返回一個(gè)double類型的隨機(jī)數(shù),也有util包里的Random類,可以生成double,int,float,long,bytes等隨機(jī)數(shù)。
但有些業(yè)務(wù)需求,往往需要對(duì)這些方法做一下封裝。比如用固定因子生成32位的3DES算法key值。
下面提供一些封裝的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package test; import java.util.Random; public class RandomUtil { public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String NUMBERCHAR = "0123456789"; /** * 返回一個(gè)定長的帶因子的固定的隨機(jī)字符串(只包含大小寫字母、數(shù)字) * * @param length * 隨機(jī)字符串長度 * @return 隨機(jī)字符串 */ public static String generateStringByKey(int length, int channel) { StringBuffer sb = new StringBuffer(); Random random = new Random(channel); for (int i = 0; i < length ; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 返回一個(gè)定長的隨機(jī)字符串(只包含大小寫字母、數(shù)字) * * @param length * 隨機(jī)字符串長度 * @return 隨機(jī)字符串 */ public static String generateString(int length) { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0 ; i < length; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 返回一個(gè)定長的隨機(jī)純字母字符串(只包含大小寫字母) * * @param length * 隨機(jī)字符串長度 * @return 隨機(jī)字符串 */ public static String generateMixString(int length) { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0 ; i < length; i++) { sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length()))); } return sb.toString(); } /** * 返回一個(gè)定長的隨機(jī)純大寫字母字符串(只包含大小寫字母) * * @param length * 隨機(jī)字符串長度 * @return 隨機(jī)字符串 */ public static String generateLowerString(int length) { return generateMixString(length).toLowerCase(); } /** * 返回一個(gè)定長的隨機(jī)純小寫字母字符串(只包含大小寫字母) * * @param length * 隨機(jī)字符串長度 * @return 隨機(jī)字符串 */ public static String generateUpperString(int length) { return generateMixString(length).toUpperCase(); } /** * 生成一個(gè)定長的純0字符串 * * @param length * 字符串長度 * @return 純0字符串 */ public static String generateZeroString(int length) { StringBuffer sb = new StringBuffer(); for (int i = 0 ; i < length; i++) { sb.append('0'); } return sb.toString(); } /** * 根據(jù)數(shù)字生成一個(gè)定長的字符串,長度不夠前面補(bǔ)0 * * @param num * 數(shù)字 * @param fixdlenth * 字符串長度 * @return 定長的字符串 */ public static String toFixdLengthString(long num, int fixdlenth) { StringBuffer sb = new StringBuffer(); String strNum = String .valueOf(num); if (fixdlenth - strNum.length() >= 0) { sb.append(generateZeroString(fixdlenth - strNum.length())); } else { throw new RuntimeException("將數(shù)字" + num + "轉(zhuǎn)化為長度為" + fixdlenth + "的字符串發(fā)生異常!"); } sb.append(strNum); return sb.toString(); } /** * 每次生成的len位數(shù)都不相同 * * @param param * @return 定長的數(shù)字 */ public static int getNotSimple(int[] param, int len) { Random rand = new Random(); for (int i = param.length; i > 1; i--) { int index = rand.nextInt(i); int tmp = param[index]; param[index] = param[i - 1]; param[i - 1] = tmp; } int result = 0; for (int i = 0; i < len; i++) { result = result * 10 + param[i]; } return result; } public static void main(String[] args) { int channel = 555555;// 測試因子比生產(chǎn)因子少1 System.out.println("返回一個(gè)定長的帶因子的固定的隨機(jī)字符串(只包含大小寫字母、數(shù)字):" + generateStringByKey(32, channel)); System.out.println("返回一個(gè)定長的隨機(jī)字符串(只包含大小寫字母、數(shù)字):" + generateString(32)); System.out.println("返回一個(gè)定長的隨機(jī)純字母字符串(只包含大小寫字母):" + generateMixString(10)); System.out.println("返回一個(gè)定長的隨機(jī)純大寫字母字符串(只包含大小寫字母):" + generateLowerString(10)); System.out.println("返回一個(gè)定長的隨機(jī)純小寫字母字符串(只包含大小寫字母):" + generateUpperString(10)); System.out.println("生成一個(gè)定長的純0字符串:" + generateZeroString(10)); System.out.println("根據(jù)數(shù)字生成一個(gè)定長的字符串,長度不夠前面補(bǔ)0:" + toFixdLengthString(123, 10)); int[] in = { 1, 2, 3, 4, 5, 6, 7 }; System.out.println("每次生成的len位數(shù)都不相同:" + getNotSimple(in, 3)); } } |
運(yùn)行效果如下:
以上這篇java隨機(jī)數(shù)生產(chǎn)算法實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/shindo/p/5995849.html