随机数字 浮点数 字符串产生
JSP中经常用到随机数字或字符(如密码产生 sessionid产生)可以使用taglib标签产生,本人使用bean产生随机数:
1.可以产生10000000 和 99999999之间随机数
2.可以产生规定数字之间的随机数,如25 100之间
3.可以使用algorithm 和 provider产生一个SecureRandom随机数字或字符串
object instead of a Random object: 71
4.可以产生浮点浮点随机数;
5.可以产生a-zA-Z0-9之间的规定长度个字符串
6.可以产生规定长度的小写字母字符串
7.可以产生任意字符串.
以下jsp bean 在Tomcat Win2000 下调试通过:
1.可以产生10000000 和 99999999之间随机数
2.可以产生规定数字之间的随机数,如25 100之间
3.可以使用algorithm 和 provider产生一个SecureRandom随机数字或字符串
object instead of a Random object: 71
4.可以产生浮点浮点随机数;
5.可以产生a-zA-Z0-9之间的规定长度个字符串
6.可以产生规定长度的小写字母字符串
7.可以产生任意字符串.
以下jsp bean 在Tomcat Win2000 下调试通过:
1
/*
2
*
3
* 随机数字bean
4
*/
5
6
package mycollect;
7
8
import java.util.*;
9
import java.security.SecureRandom;
10
import java.security.NoSuchAlgorithmException;
11
import java.security.NoSuchProviderException;
12
13
14
public class RandomNum {
15
16
private Long randomnum = null;
17
private Float randomfloat = null;
18
private boolean floatvalue = false;
19
private long upper = 100;
20
private long lower = 0;
21
private String algorithm = null;
22
private String provider = null;
23
private boolean secure = false;
24
private Random random = null;
25
private SecureRandom secrandom = null;
26
27
private final float getFloat() {
28
if (random == null)
29
return secrandom.nextFloat();
30
else
31
return random.nextFloat();
32
}
33
34
public final void generateRandomObject() throws Exception {
35
36
// check to see if the object is a SecureRandom object
37
if (secure) {
38
try {
39
// get an instance of a SecureRandom object
40
if (provider != null)
41
// search for algorithm in package provider
42
secrandom = SecureRandom.getInstance(algorithm, provider);
43
else
44
secrandom = SecureRandom.getInstance(algorithm);
45
} catch (NoSuchAlgorithmException ne) {
46
throw new Exception(ne.getMessage());
47
} catch (NoSuchProviderException pe) {
48
throw new Exception(pe.getMessage());
49
}
50
} else
51
random = new Random();
52
}
53
54
/**
55
* generate the random number
56
*
57
*/
58
private final void generaterandom() {
59
60
int tmprandom = 0; // temp storage for random generated number
61
Integer rand;
62
63
// check to see if float value is expected
64
if (floatvalue)
65
randomfloat = new Float(getFloat());
66
else
67
randomnum = new Long(lower + (long) ((getFloat() * (upper - lower))));
68
}
69
70
public final Number getRandom() {
71
generaterandom();
72
if (floatvalue)
73
return randomfloat;
74
else
75
return randomnum;
76
}
77
78
public final void setRange(long low, long up) {
79
80
// set the upper and lower bound of the range
81
lower = low;
82
upper = up;
83
84
// check to see if a float value is expected
85
if ((lower == 0) && (upper == 1))
86
floatvalue = true;
87
}
88
89
/**
90
* set the algorithm name
91
*
92
* @param value name of the algorithm to use for a SecureRandom object
93
*
94
*/
95
public final void setAlgorithm(String value) {
96
algorithm = value;
97
secure = true; // a SecureRandom object is to be used
98
}
99
100
public final void setProvider(String value)
101
{
102
provider = value;
103
}
104
105
public final void setRange(String value) throws Exception
106
{
107
try
108
{
109
upper = new Integer(value.substring(value.indexOf('-') + 1)).longValue();
110
} catch (Exception ex) {
111
throw new Exception("upper attribute could not be" +
112
" turned into an Integer default value was used");
113
}
114
115
try
116
{
117
lower = new Integer(value.substring(0, value.indexOf('-'))).longValue();
118
} catch (Exception ex) {
119
throw new Exception("lower attribute could not be" +
120
" turned into an Integer default value was used");
121
}
122
123
if ((lower == 0) && (upper == 1))
124
floatvalue = true;
125
126
if (upper < lower)
127
throw new Exception("You can't have a range where the lowerbound" +
128
" is higher than the upperbound.");
129
130
}
131
132
133
}
134
随机字符串bean
/*2
*3
* 随机数字bean4
*/ 5

6
package mycollect;7

8
import java.util.*;9
import java.security.SecureRandom;10
import java.security.NoSuchAlgorithmException;11
import java.security.NoSuchProviderException;12

13

14
public class RandomNum {15

16
private Long randomnum = null;17
private Float randomfloat = null;18
private boolean floatvalue = false;19
private long upper = 100;20
private long lower = 0;21
private String algorithm = null;22
private String provider = null;23
private boolean secure = false;24
private Random random = null;25
private SecureRandom secrandom = null;26

27
private final float getFloat() {28
if (random == null)29
return secrandom.nextFloat();30
else31
return random.nextFloat();32
}33

34
public final void generateRandomObject() throws Exception {35

36
// check to see if the object is a SecureRandom object37
if (secure) {38
try {39
// get an instance of a SecureRandom object40
if (provider != null)41
// search for algorithm in package provider42
secrandom = SecureRandom.getInstance(algorithm, provider);43
else44
secrandom = SecureRandom.getInstance(algorithm);45
} catch (NoSuchAlgorithmException ne) {46
throw new Exception(ne.getMessage());47
} catch (NoSuchProviderException pe) {48
throw new Exception(pe.getMessage());49
}50
} else51
random = new Random();52
}53

54
/**55
* generate the random number56
*57
*/58
private final void generaterandom() {59

60
int tmprandom = 0; // temp storage for random generated number61
Integer rand;62

63
// check to see if float value is expected64
if (floatvalue)65
randomfloat = new Float(getFloat());66
else67
randomnum = new Long(lower + (long) ((getFloat() * (upper - lower))));68
}69

70
public final Number getRandom() {71
generaterandom(); 72
if (floatvalue)73
return randomfloat;74
else75
return randomnum;76
}77

78
public final void setRange(long low, long up) {79

80
// set the upper and lower bound of the range81
lower = low;82
upper = up;83

84
// check to see if a float value is expected85
if ((lower == 0) && (upper == 1))86
floatvalue = true;87
}88

89
/**90
* set the algorithm name91
*92
* @param value name of the algorithm to use for a SecureRandom object93
*94
*/95
public final void setAlgorithm(String value) {96
algorithm = value;97
secure = true; // a SecureRandom object is to be used98
}99

100
public final void setProvider(String value) 101
{102
provider = value;103
}104

105
public final void setRange(String value) throws Exception 106
{107
try 108
{109
upper = new Integer(value.substring(value.indexOf('-') + 1)).longValue();110
} catch (Exception ex) {111
throw new Exception("upper attribute could not be" +112
" turned into an Integer default value was used"); 113
}114

115
try 116
{117
lower = new Integer(value.substring(0, value.indexOf('-'))).longValue();118
} catch (Exception ex) {119
throw new Exception("lower attribute could not be" +120
" turned into an Integer default value was used"); 121
}122

123
if ((lower == 0) && (upper == 1))124
floatvalue = true;125

126
if (upper < lower)127
throw new Exception("You can't have a range where the lowerbound" +128
" is higher than the upperbound.");129

130
}131

132

133
}134

1
package mycollect;
2
3
import java.util.*;
4
import java.security.SecureRandom;
5
import java.security.NoSuchAlgorithmException;
6
import java.security.NoSuchProviderException;
7
8
public class RandomStrg {
9
10
private String randomstr;
11
private boolean allchars = false;
12
//欠缺是8位字符串
13
private Integer length = new Integer(8);
14
private HashMap hmap;
15
private ArrayList lower = null;
16
private ArrayList upper = null;
17
private char[] single = null;
18
private int singlecount = 0;
19
private boolean singles = false;
20
private String algorithm = null;
21
private String provider = null;
22
private boolean secure = false;
23
private Random random = null;
24
private SecureRandom secrandom = null;
25
26
private final float getFloat() {
27
if (random == null)
28
return secrandom.nextFloat();
29
else
30
return random.nextFloat();
31
}
32
33
/**
34
* generate the Random object that will be used for this random number
35
* generator
36
*
37
*/
38
public final void generateRandomObject() throws Exception {
39
40
// check to see if the object is a SecureRandom object
41
if (secure) {
42
try {
43
// get an instance of a SecureRandom object
44
if (provider != null)
45
// search for algorithm in package provider
46
random = SecureRandom.getInstance(algorithm, provider);
47
else
48
random = SecureRandom.getInstance(algorithm);
49
} catch (NoSuchAlgorithmException ne) {
50
throw new Exception(ne.getMessage());
51
} catch (NoSuchProviderException pe) {
52
throw new Exception(pe.getMessage());
53
}
54
} else
55
random = new Random();
56
}
57
58
/**
59
* generate the random string
60
*
61
*/
62
private final void generaterandom() {
63
64
if (allchars)
65
for (int i = 0; i < length.intValue(); i++)
66
randomstr = randomstr + new Character((char)((int) 34 +
67
((int)(getFloat() * 93)))).toString();
68
else if (singles) {
69
// check if there are single chars to be included
70
71
if (upper.size() == 3) {
72
// check for the number of ranges max 3 uppercase lowercase digits
73
74
// build the random string
75
for (int i = 0; i < length.intValue(); i++) {
76
// you have four groups to choose a random number from, to make
77
// the choice a little more random select a number out of 100
78
79
// get a random number even or odd
80
if (((int) (getFloat() * 100)) % 2 == 0) {
81
82
// the number was even get another number even or odd
83
if (((int) (getFloat() * 100)) % 2 == 0)
84
// choose a random char from the single char group
85
randomstr = randomstr + randomSingle().toString();
86
else
87
// get a random char from the first range
88
randomstr = randomstr + randomChar((Character)lower.get(2),
89
(Character)upper.get(2)).toString();
90
} else {
91
// the number was odd
92
93
if (((int) (getFloat() * 100)) % 2 == 0)
94
// choose a random char from the second range
95
randomstr = randomstr + randomChar((Character)lower.get(1),
96
(Character)upper.get(1)).toString();
97
else
98
// choose a random char from the third range
99
randomstr = randomstr + randomChar((Character)lower.get(0),
100
(Character)upper.get(0)).toString();
101
}
102
}
103
} else if (upper.size() == 2) {
104
// single chars are to be included choose a random char from
105
// two different ranges
106
107
// build the random char from single chars and two ranges
108
for (int i = 0; i < length.intValue(); i++) {
109
// select the single chars or a range to get each random char
110
// from
111
112
if (((int)(getFloat() * 100)) % 2 == 0) {
113
114
// get random char from the single chars
115
randomstr = randomstr + randomSingle().toString();
116
} else if (((int) (getFloat() * 100)) % 2 == 0) {
117
118
// get the random char from the first range
119
randomstr = randomstr + randomChar((Character)lower.get(1),
120
(Character)upper.get(1)).toString();
121
} else {
122
123
// get the random char from the second range
124
randomstr = randomstr + randomChar((Character)lower.get(0),
125
(Character)upper.get(0)).toString();
126
}
127
}
128
} else if (upper.size() == 1) {
129
130
// build the random string from single chars and one range
131
for (int i = 0; i < length.intValue(); i++) {
132
if (((int) getFloat() * 100) % 2 == 0)
133
// get a random single char
134
randomstr = randomstr + randomSingle().toString();
135
else
136
// get a random char from the range
137
randomstr = randomstr + randomChar((Character)lower.get(0),
138
(Character)upper.get(0)).toString();
139
}
140
} else {
141
// build the rand string from single chars
142
for (int i = 0; i < length.intValue(); i++)
143
randomstr = randomstr + randomSingle().toString();
144
}
145
} else {
146
147
// no single chars are to be included in the random string
148
if (upper.size() == 3) {
149
150
// build random strng from three ranges
151
for (int i = 0; i < length.intValue(); i++) {
152
153
if (((int) (getFloat() * 100)) % 2 == 0) {
154
155
// get random char from first range
156
randomstr = randomstr + randomChar((Character)lower.get(2),
157
(Character)upper.get(2)).toString();
158
} else if (((int) (getFloat() * 100)) % 2 == 0) {
159
160
// get random char form second range
161
randomstr = randomstr + randomChar((Character)lower.get(1),
162
(Character)upper.get(1)).toString();
163
} else {
164
165
// get random char from third range
166
randomstr = randomstr + randomChar((Character)lower.get(0),
167
(Character)upper.get(0)).toString();
168
}
169
}
170
} else if (upper.size() == 2) {
171
172
// build random string from two ranges
173
for (int i = 0; i < length.intValue(); i++) {
174
if (((int) (getFloat() * 100)) % 2 == 0)
175
// get random char from first range
176
randomstr = randomstr + randomChar((Character)lower.get(1),
177
(Character)upper.get(1)).toString();
178
else
179
// get random char from second range
180
randomstr = randomstr + randomChar((Character)lower.get(0),
181
(Character)upper.get(0)).toString();
182
}
183
} else
184
185
// build random string
186
for (int i = 0; i < length.intValue(); i++)
187
// get random char from only range
188
randomstr = randomstr + randomChar((Character)lower.get(0),
189
(Character)upper.get(0)).toString();
190
}
191
}
192
193
/**
194
* generate a random char from the single char list
195
*
196
* @returns - a randomly selscted character from the single char list
197
*
198
*/
199
private final Character randomSingle() {
200
201
return (new Character(single[(int)((getFloat() * singlecount) - 1)]));
202
}
203
204
/**
205
* generate a random character
206
*
207
* @param lower lower bound from which to get a random char
208
* @param upper upper bound from which to get a random char
209
*
210
* @returns - a randomly generated character
211
*
212
*/
213
private final Character randomChar(Character lower, Character upper) {
214
int tempval;
215
char low = lower.charValue();
216
char up = upper.charValue();
217
218
// get a random number in the range lowlow - lowup
219
tempval = (int)((int)low + (getFloat() * ((int)(up - low))));
220
221
// return the random char
222
return (new Character((char) tempval));
223
}
224
225
/**
226
* get the randomly created string for use with the
227
* <jsp:getProperty name=<i>"id"</i> property="randomstr"/>
228
*
229
* @return - randomly created string
230
*
231
*/
232
public final String getRandom() {
233
234
randomstr = new String();
235
236
generaterandom(); // generate the first random string
237
238
if (hmap != null) {
239
240
while (hmap.containsKey(randomstr)) {
241
// random string has already been created generate a different one
242
generaterandom();
243
}
244
245
hmap.put(randomstr, null); // add the new random string
246
}
247
248
return randomstr;
249
}
250
251
/**
252
* set the ranges from which to choose the characters for the random string
253
*
254
* @param low set of lower ranges
255
* @param up set of upper ranges
256
*
257
*/
258
public final void setRanges(ArrayList low, ArrayList up) {
259
lower = low;
260
upper = up;
261
}
262
263
264
/**
265
* set the hashmap that is used to check the uniqueness of random strings
266
*
267
* @param map hashmap whose keys are used to insure uniqueness of random strgs
268
*
269
*/
270
public final void setHmap(HashMap map) {
271
hmap = map;
272
}
273
274
/**
275
* set the length of the random string
276
*
277
* @param value length of the random string
278
*
279
*/
280
public final void setLength(String value) {
281
length = new Integer(value);
282
283
}
284
285
/**
286
* set the algorithm name
287
*
288
* @param value name of the algorithm to use for a SecureRandom object
289
*
290
*/
291
public final void setAlgorithm(String value) {
292
algorithm = value;
293
secure = true; // a SecureRandom object is to be used
294
}
295
296
/**
297
* set the provider name
298
*
299
* @param value name of the package to check for the algorithm
300
*
301
*/
302
public final void setProvider(String value) {
303
provider = value;
304
}
305
306
/**
307
* set the allchars flag
308
*
309
* @param value boolean value of the allchars flag
310
*
311
*/
312
public final void setAllchars(boolean value) {
313
allchars = value;
314
}
315
316
/**
317
* set the array of single chars to choose from for this random string and the
318
* number of chars in the array
319
*
320
* @param chars the array of single chars
321
* @param value the number of single chars
322
*
323
*/
324
public final void setSingle(char[] chars, int value) {
325
single = chars; // set the array of chars
326
singlecount = value; // set the number of chars in array single
327
singles = true; // set flag that single chars are in use
328
}
329
330
public final void setCharset(String value)
331
{
332
// values tells the method whether or not to check for single chars
333
boolean more = true;
334
335
// create the arraylists to hold the upper and lower bounds for the char
336
// ranges
337
lower = new ArrayList(3);
338
upper = new ArrayList(3);
339
340
// user has chosen to use all possible characters in the random string
341
if (value.compareTo("all") == 0) {
342
allchars = true; // set allchars flag
343
// all chars are to be used so there are no single chars to sort
344
// through
345
more = false;
346
}else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\')) {
347
// run through the ranges at most 3
348
while (more && (value.charAt(1) == '-')){
349
350
// check to make sure that the dash is not the single char
351
if (value.charAt(0) == '\\')
352
break;
353
else {
354
// add upper and lower ranges to there list
355
lower.add(new Character(value.charAt(0)));
356
upper.add(new Character(value.charAt(2)));
357
}
358
359
// check to see if there is more to the charset
360
if (value.length() <= 3)
361
more = false;
362
else
363
// create a new string so that the next range if there is one
364
// starts it
365
value = value.substring(3);
366
}
367
}
368
369
// if more = false there are no single chars in the charset
370
if (more) {
371
372
single = new char[30]; // create single
373
374
// create a set of tokens from the string of single chars
375
StringTokenizer tokens = new StringTokenizer(value);
376
377
while (tokens.hasMoreTokens()) {
378
// get the next token from the string
379
String token = tokens.nextToken();
380
381
if (token.length() > 1)
382
// char is a - add it to the list
383
single[singlecount++] = '-';
384
385
// add the current char to the list
386
single[singlecount++] = token.charAt(0);
387
}
388
}
389
if ((lower == null) && (single == null))
390
setCharset("a-zA-Z0-9");
391
}
392
}
393
JSP调用语句:
package mycollect; 2

3
import java.util.*;4
import java.security.SecureRandom;5
import java.security.NoSuchAlgorithmException;6
import java.security.NoSuchProviderException;7

8
public class RandomStrg {9

10
private String randomstr;11
private boolean allchars = false;12
//欠缺是8位字符串 13
private Integer length = new Integer(8);14
private HashMap hmap;15
private ArrayList lower = null;16
private ArrayList upper = null;17
private char[] single = null;18
private int singlecount = 0;19
private boolean singles = false;20
private String algorithm = null;21
private String provider = null;22
private boolean secure = false;23
private Random random = null;24
private SecureRandom secrandom = null;25

26
private final float getFloat() {27
if (random == null)28
return secrandom.nextFloat();29
else30
return random.nextFloat();31
}32

33
/**34
* generate the Random object that will be used for this random number 35
* generator36
*37
*/38
public final void generateRandomObject() throws Exception {39

40
// check to see if the object is a SecureRandom object41
if (secure) {42
try {43
// get an instance of a SecureRandom object44
if (provider != null)45
// search for algorithm in package provider46
random = SecureRandom.getInstance(algorithm, provider);47
else48
random = SecureRandom.getInstance(algorithm);49
} catch (NoSuchAlgorithmException ne) {50
throw new Exception(ne.getMessage());51
} catch (NoSuchProviderException pe) {52
throw new Exception(pe.getMessage());53
}54
} else55
random = new Random();56
}57

58
/**59
* generate the random string60
*61
*/62
private final void generaterandom() {63

64
if (allchars)65
for (int i = 0; i < length.intValue(); i++)66
randomstr = randomstr + new Character((char)((int) 34 + 67
((int)(getFloat() * 93)))).toString();68
else if (singles) {69
// check if there are single chars to be included70

71
if (upper.size() == 3) {72
// check for the number of ranges max 3 uppercase lowercase digits73

74
// build the random string75
for (int i = 0; i < length.intValue(); i++) {76
// you have four groups to choose a random number from, to make77
// the choice a little more random select a number out of 10078

79
// get a random number even or odd80
if (((int) (getFloat() * 100)) % 2 == 0) {81

82
// the number was even get another number even or odd83
if (((int) (getFloat() * 100)) % 2 == 0)84
// choose a random char from the single char group85
randomstr = randomstr + randomSingle().toString();86
else87
// get a random char from the first range88
randomstr = randomstr + randomChar((Character)lower.get(2),89
(Character)upper.get(2)).toString();90
} else {91
// the number was odd92

93
if (((int) (getFloat() * 100)) % 2 == 0)94
// choose a random char from the second range95
randomstr = randomstr + randomChar((Character)lower.get(1),96
(Character)upper.get(1)).toString();97
else98
// choose a random char from the third range99
randomstr = randomstr + randomChar((Character)lower.get(0), 100
(Character)upper.get(0)).toString();101
}102
}103
} else if (upper.size() == 2) {104
// single chars are to be included choose a random char from105
// two different ranges106

107
// build the random char from single chars and two ranges108
for (int i = 0; i < length.intValue(); i++) {109
// select the single chars or a range to get each random char110
// from111

112
if (((int)(getFloat() * 100)) % 2 == 0) {113

114
// get random char from the single chars115
randomstr = randomstr + randomSingle().toString();116
} else if (((int) (getFloat() * 100)) % 2 == 0) {117

118
// get the random char from the first range119
randomstr = randomstr + randomChar((Character)lower.get(1),120
(Character)upper.get(1)).toString();121
} else {122

123
// get the random char from the second range124
randomstr = randomstr + randomChar((Character)lower.get(0),125
(Character)upper.get(0)).toString();126
}127
}128
} else if (upper.size() == 1) {129

130
// build the random string from single chars and one range131
for (int i = 0; i < length.intValue(); i++) {132
if (((int) getFloat() * 100) % 2 == 0)133
// get a random single char134
randomstr = randomstr + randomSingle().toString();135
else136
// get a random char from the range137
randomstr = randomstr + randomChar((Character)lower.get(0),138
(Character)upper.get(0)).toString();139
}140
} else {141
// build the rand string from single chars142
for (int i = 0; i < length.intValue(); i++)143
randomstr = randomstr + randomSingle().toString();144
}145
} else {146

147
// no single chars are to be included in the random string148
if (upper.size() == 3) {149

150
// build random strng from three ranges151
for (int i = 0; i < length.intValue(); i++) {152

153
if (((int) (getFloat() * 100)) % 2 == 0) {154

155
// get random char from first range156
randomstr = randomstr + randomChar((Character)lower.get(2),157
(Character)upper.get(2)).toString();158
} else if (((int) (getFloat() * 100)) % 2 == 0) {159

160
// get random char form second range161
randomstr = randomstr + randomChar((Character)lower.get(1),162
(Character)upper.get(1)).toString();163
} else {164

165
// get random char from third range166
randomstr = randomstr + randomChar((Character)lower.get(0),167
(Character)upper.get(0)).toString();168
}169
}170
} else if (upper.size() == 2) {171

172
// build random string from two ranges173
for (int i = 0; i < length.intValue(); i++) {174
if (((int) (getFloat() * 100)) % 2 == 0)175
// get random char from first range176
randomstr = randomstr + randomChar((Character)lower.get(1),177
(Character)upper.get(1)).toString();178
else179
// get random char from second range180
randomstr = randomstr + randomChar((Character)lower.get(0),181
(Character)upper.get(0)).toString();182
}183
} else184

185
// build random string186
for (int i = 0; i < length.intValue(); i++)187
// get random char from only range188
randomstr = randomstr + randomChar((Character)lower.get(0), 189
(Character)upper.get(0)).toString();190
}191
}192

193
/**194
* generate a random char from the single char list195
*196
* @returns - a randomly selscted character from the single char list197
*198
*/199
private final Character randomSingle() {200

201
return (new Character(single[(int)((getFloat() * singlecount) - 1)]));202
}203

204
/**205
* generate a random character206
*207
* @param lower lower bound from which to get a random char208
* @param upper upper bound from which to get a random char209
*210
* @returns - a randomly generated character211
*212
*/213
private final Character randomChar(Character lower, Character upper) {214
int tempval;215
char low = lower.charValue();216
char up = upper.charValue();217

218
// get a random number in the range lowlow - lowup219
tempval = (int)((int)low + (getFloat() * ((int)(up - low))));220

221
// return the random char222
return (new Character((char) tempval));223
}224

225
/**226
* get the randomly created string for use with the 227
* <jsp:getProperty name=<i>"id"</i> property="randomstr"/>228
*229
* @return - randomly created string230
*231
*/232
public final String getRandom() {233

234
randomstr = new String();235

236
generaterandom(); // generate the first random string237

238
if (hmap != null) {239

240
while (hmap.containsKey(randomstr)) {241
// random string has already been created generate a different one242
generaterandom();243
}244

245
hmap.put(randomstr, null); // add the new random string246
}247

248
return randomstr;249
}250

251
/**252
* set the ranges from which to choose the characters for the random string253
*254
* @param low set of lower ranges255
* @param up set of upper ranges256
*257
*/258
public final void setRanges(ArrayList low, ArrayList up) {259
lower = low;260
upper = up;261
}262

263

264
/**265
* set the hashmap that is used to check the uniqueness of random strings266
*267
* @param map hashmap whose keys are used to insure uniqueness of random strgs268
*269
*/270
public final void setHmap(HashMap map) {271
hmap = map;272
}273

274
/**275
* set the length of the random string276
*277
* @param value length of the random string278
*279
*/280
public final void setLength(String value) {281
length = new Integer(value);282

283
}284

285
/**286
* set the algorithm name287
*288
* @param value name of the algorithm to use for a SecureRandom object289
*290
*/291
public final void setAlgorithm(String value) {292
algorithm = value;293
secure = true; // a SecureRandom object is to be used294
}295

296
/**297
* set the provider name298
*299
* @param value name of the package to check for the algorithm300
*301
*/302
public final void setProvider(String value) {303
provider = value;304
}305

306
/**307
* set the allchars flag308
*309
* @param value boolean value of the allchars flag310
*311
*/312
public final void setAllchars(boolean value) {313
allchars = value;314
}315

316
/**317
* set the array of single chars to choose from for this random string and the318
* number of chars in the array319
*320
* @param chars the array of single chars321
* @param value the number of single chars322
*323
*/324
public final void setSingle(char[] chars, int value) {325
single = chars; // set the array of chars326
singlecount = value; // set the number of chars in array single327
singles = true; // set flag that single chars are in use328
}329

330
public final void setCharset(String value) 331
{332
// values tells the method whether or not to check for single chars333
boolean more = true;334

335
// create the arraylists to hold the upper and lower bounds for the char336
// ranges337
lower = new ArrayList(3);338
upper = new ArrayList(3);339

340
// user has chosen to use all possible characters in the random string341
if (value.compareTo("all") == 0) {342
allchars = true; // set allchars flag343
// all chars are to be used so there are no single chars to sort 344
// through345
more = false;346
}else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\')) {347
// run through the ranges at most 3 348
while (more && (value.charAt(1) == '-')){349

350
// check to make sure that the dash is not the single char351
if (value.charAt(0) == '\\')352
break;353
else {354
// add upper and lower ranges to there list355
lower.add(new Character(value.charAt(0)));356
upper.add(new Character(value.charAt(2)));357
}358

359
// check to see if there is more to the charset360
if (value.length() <= 3)361
more = false;362
else363
// create a new string so that the next range if there is one364
// starts it365
value = value.substring(3);366
}367
}368

369
// if more = false there are no single chars in the charset370
if (more) {371

372
single = new char[30]; // create single373

374
// create a set of tokens from the string of single chars375
StringTokenizer tokens = new StringTokenizer(value);376

377
while (tokens.hasMoreTokens()) {378
// get the next token from the string379
String token = tokens.nextToken();380

381
if (token.length() > 1)382
// char is a - add it to the list383
single[singlecount++] = '-';384

385
// add the current char to the list386
single[singlecount++] = token.charAt(0);387
}388
}389
if ((lower == null) && (single == null))390
setCharset("a-zA-Z0-9"); 391
}392
}393

1
<%@ page contentType="text/html;charset=ISO8859_1" %>
2
<%@ page import="java.util.*" %>
3
<jsp:useBean id="RNUM" scope="page" class="mycollect.RandomNum" />
4
<jsp:useBean id="RSTR" scope="page" class="mycollect.RandomStrg" />
5
6
<html>
7
<head>
8
<title>随机数字 浮点数 字符串</title>
9
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
10
</head>
11
12
<body>
13
<h3>随机数字 浮点数 字符串</h3>
14
<%
15
//Random generator = new Random();
16
//int limit = 10;
17
//int randomNumber = (int)(generator.nextDouble() * limit);
18
19
20
out.println("<p>创建在10000000 和 99999999之间的随机数:");
21
RNUM.setRange("10000000-99999999");
22
RNUM.generateRandomObject();
23
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");
24
25
out.println("<p>在n 25 和 100之间创建一个随机数:");
26
RNUM.setRange("25-100");
27
RNUM.generateRandomObject();
28
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");
29
30
%>
31
<p>Create the same random number between 25 and 100, only use the <br>
32
algorithm and provider attributes to indicate the use of a SecureRandom<br>
33
object instead of a Random object:
34
<%
35
RNUM.setRange("25-100");
36
RNUM.setAlgorithm("SHA1PRNG");
37
RNUM.setProvider("SUN");
38
RNUM.generateRandomObject();
39
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");
40
41
out.println("<p>Create a random float value:");
42
RNUM.setRange("0-1");
43
RNUM.generateRandomObject();
44
String radio= new java.text.DecimalFormat("###.##########").format(RNUM.getRandom());
45
out.println("<b>"+radio+"</b>");
46
47
out.println("<p>===========================================");
48
49
out.println("<p>在a-zA-Z0-9之间,也就是数字和26个字母混合的随机字符串,(欠缺是8位,该功能适合创建随机密码和sessionid)");
50
RSTR.setCharset("a-zA-Z0-9");
51
RSTR.generateRandomObject();
52
out.println("<b>"+RSTR.getRandom()+"</b>");
53
54
55
out.println("<p>Create a random string 15 lowercase letters long:");
56
RSTR.setCharset("a-z");
57
RSTR.setLength("15");
58
RSTR.generateRandomObject();
59
out.println("<b>"+RSTR.getRandom()+"</b>");
60
61
out.println("<p>Create a random string with only caps:");
62
RSTR.setCharset("A-Z");
63
RSTR.generateRandomObject();
64
out.println("<b>"+RSTR.getRandom()+"</b>");
65
66
out.println("<p>Create a random string 10 characters long with the charset a-fF-K ! \\ $ % # ^ - * ? notice that the - and had to be escaped with a :");
67
RSTR.setCharset("a-fF-K!\\$%#^-*?");
68
RSTR.setLength("10");
69
RSTR.generateRandomObject();
70
out.println("<b>"+RSTR.getRandom()+"</b>");
71
72
73
out.println("<p>Create a random string of all the characters and digits:");
74
RSTR.setCharset("all");
75
RSTR.generateRandomObject();
76
out.println("<b>"+RSTR.getRandom()+" </b>");
77
78
%><p>
79
Create the same random string of all the characters and digits, only use<br>
80
the algorithm and provider attributes to indicate the use of a SecureRandom<br>
81
object instead of a Random object:
82
<%
83
RSTR.setCharset("all");
84
RSTR.setAlgorithm("SHA1PRNG");
85
RSTR.setProvider("SUN");
86
RSTR.generateRandomObject();
87
out.println("<b>"+RSTR.getRandom()+"</b>");
88
89
%>
90
91
</body>
92
</html>
93
<%@ page contentType="text/html;charset=ISO8859_1" %> 2
<%@ page import="java.util.*" %> 3
<jsp:useBean id="RNUM" scope="page" class="mycollect.RandomNum" />4
<jsp:useBean id="RSTR" scope="page" class="mycollect.RandomStrg" />5

6
<html>7
<head>8
<title>随机数字 浮点数 字符串</title>9
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">10
</head>11

12
<body>13
<h3>随机数字 浮点数 字符串</h3>14
<%15
//Random generator = new Random();16
//int limit = 10;17
//int randomNumber = (int)(generator.nextDouble() * limit);18

19

20
out.println("<p>创建在10000000 和 99999999之间的随机数:");21
RNUM.setRange("10000000-99999999");22
RNUM.generateRandomObject();23
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");24

25
out.println("<p>在n 25 和 100之间创建一个随机数:");26
RNUM.setRange("25-100");27
RNUM.generateRandomObject();28
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");29

30
%>31
<p>Create the same random number between 25 and 100, only use the <br>32
algorithm and provider attributes to indicate the use of a SecureRandom<br>33
object instead of a Random object:34
<% 35
RNUM.setRange("25-100");36
RNUM.setAlgorithm("SHA1PRNG");37
RNUM.setProvider("SUN");38
RNUM.generateRandomObject();39
out.println("<b>"+RNUM.getRandom().intValue()+"</b>");40

41
out.println("<p>Create a random float value:");42
RNUM.setRange("0-1");43
RNUM.generateRandomObject();44
String radio= new java.text.DecimalFormat("###.##########").format(RNUM.getRandom());45
out.println("<b>"+radio+"</b>");46

47
out.println("<p>===========================================");48

49
out.println("<p>在a-zA-Z0-9之间,也就是数字和26个字母混合的随机字符串,(欠缺是8位,该功能适合创建随机密码和sessionid)");50
RSTR.setCharset("a-zA-Z0-9");51
RSTR.generateRandomObject();52
out.println("<b>"+RSTR.getRandom()+"</b>");53

54

55
out.println("<p>Create a random string 15 lowercase letters long:");56
RSTR.setCharset("a-z");57
RSTR.setLength("15");58
RSTR.generateRandomObject();59
out.println("<b>"+RSTR.getRandom()+"</b>");60

61
out.println("<p>Create a random string with only caps:");62
RSTR.setCharset("A-Z");63
RSTR.generateRandomObject();64
out.println("<b>"+RSTR.getRandom()+"</b>");65

66
out.println("<p>Create a random string 10 characters long with the charset a-fF-K ! \\ $ % # ^ - * ? notice that the - and had to be escaped with a :");67
RSTR.setCharset("a-fF-K!\\$%#^-*?");68
RSTR.setLength("10");69
RSTR.generateRandomObject();70
out.println("<b>"+RSTR.getRandom()+"</b>");71

72

73
out.println("<p>Create a random string of all the characters and digits:");74
RSTR.setCharset("all");75
RSTR.generateRandomObject();76
out.println("<b>"+RSTR.getRandom()+" </b>");77

78
%><p>79
Create the same random string of all the characters and digits, only use<br>80
the algorithm and provider attributes to indicate the use of a SecureRandom<br>81
object instead of a Random object:82
<%83
RSTR.setCharset("all");84
RSTR.setAlgorithm("SHA1PRNG");85
RSTR.setProvider("SUN");86
RSTR.generateRandomObject();87
out.println("<b>"+RSTR.getRandom()+"</b>");88

89
%>90

91
</body>92
</html>93





浙公网安备 33010602011771号