- public class RandomString {
- public static String randomstring(int lo, int hi){
- int n = rand(lo, hi);
- byte b[] = new byte[n];
- for (int i = 0; i < n; i++)
- b[i] = (byte)rand('a', 'z');
- return new String(b, 0);
- }
- private static int rand(int lo, int hi){
- java.util.Random rn = new java.util.Random();
- int n = hi - lo + 1;
- int i = rn.nextInt(n);
- if (i < 0)
- i = -i;
- return lo + i;
- }
- public static String randomstring(){
- return randomstring(5, 25);
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- System.out.println(randomstring());
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/020920135526.html
来源: http://www.codesnippet.cn/detail/020920135526.html