Pages

2011-03-29

Create a random string

import java.util.Random;
/**
* @author E. Kaparulin
*/
public class RandomString {
        private final char[] alphanumeric=alphanumeric();
            private final Random rand;
            public RandomString(){this(null);}
            public RandomString(Random rand){
              this.rand=(rand!=null) ? rand : new Random();
            }
            public String get(int len){
                StringBuffer out=new StringBuffer();
                while(out.length() < len){
                    int idx=Math.abs(( rand.nextInt() % alphanumeric.length ));
                    out.append(alphanumeric[idx]);
                }
                return out.toString();
            }
            // create alphanumeric char array
            private char[] alphanumeric(){
                StringBuffer buf=new StringBuffer(128);
                for(int i=48; i<= 57;i++)buf.append((char)i); // 0-9
                for(int i=65; i<= 90;i++)buf.append((char)i); // A-Z
                for(int i=97; i<=122;i++)buf.append((char)i); // a-z
                return buf.toString().toCharArray();
            }
}

No comments:

Post a Comment