private static String getRandomString ( int length , char [ ] chars ) throws IllegalArgumentException { if ( length < 1 ) throw new IllegalArgumentException ( "Invalid length: " + length ) ; if ( chars == null || chars . length == 0 ) throw new IllegalArgumentException ( "Null/Empty chars" ) ; StringBuilder sb = new StringBuilder ( ) ; Random random = new Random ( ) ; for ( int i = 0 ; i < length ; i ++ ) { char c = chars [ random . nextInt ( chars . length ) ] ; sb . append ( c ) ; } return sb . toString ( ) ; }
Generates a random String