Apache came with implementation in creating a random value using the RandomStringUtils class.
RandomStringUtils : Operations for random
Strings
.Creates a random string whose length is the number of characters specified.
public static String random(int count, String chars)
- Creates a random string whose length is the number of characters specified. Characters will be chosen from the set of characters specified.
-
- Parameters:
count
- the length of random string to createchars
- the String containing the set of characters to use, may be null- Returns:
- the random string
- Throws:
IllegalArgumentException
- ifcount
< 0.
Example: RandomStringUtils.random(6, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-*/!@#$%^&*()");
The above example gives the random string whose length is 6.
Refer the URL for more details http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/RandomStringUtils.html
No comments :
Post a Comment